Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define vi vector<int>
- #define pb push_back
- #define mp make_pair
- #define ii pair<int,int>
- #define ll long long int
- #define N 1305
- #define oo 1e5
- #define pil pair<int,int>
- int getVertex(char a)
- {
- if(a == 'A'){
- return 1;
- }
- return (a % 65)+1;
- }
- char getCharVertex(int a)
- {
- if(a == 1){
- return 'A';
- }
- return 'A' + (a-1);
- }
- int n;
- vector<vector<pil>> graph(N);
- vector<vector<int>> dist(N,vector<int>(N,oo));
- void FloydWarshal(){
- for(int i=1;i<=n;i++){
- dist[i][i] = 0;
- for(pil aresta : graph[i]){
- dist[i][aresta.first] = aresta.second;
- }
- }
- for(int k=1; k<=n; k++){
- for(int i=1; i<=n; i++){
- for(int j=1; j<=n; j++){
- if(dist[i][j] > dist[i][k] + dist[k][j]){
- dist[i][j] = dist[i][k] + dist[k][j];
- }
- }
- }
- }
- }
- pil PegarRodoviaProxima(int i1,int i2)
- {
- for(int j=1;j<n;j++){
- if((dist[i1][j] != 0)&&(dist[i2][j]!=0)&&(dist[i1][j]!=oo)&&(dist[i2][j]!=oo)){
- if(((dist[i1][j]%79) == 0)&&((dist[i2][j]%84) == 0)){
- return mp(j,(dist[i1][j]/79)+(dist[i2][j]/84));
- }
- }
- }
- return mp(-1,oo);
- }
- int main(){
- char veiculo;
- char tipo;
- char va,vb;
- int a,b,peso;
- int norm;
- char Davis,Andre;
- scanf("%d",&n);
- for(int i=0;i<n;i++)
- {
- scanf("\n%c %c %c %c %d",&veiculo,&tipo,&va,&vb,&peso);
- a = getVertex(va);
- b = getVertex(vb);
- norm = (int)veiculo;
- if(tipo == 'B'){ //via unidirecional(U) ou bidirecional(B)
- graph[a].pb(mp(b,peso*norm));
- graph[b].pb(mp(a,peso*norm));
- }else if(tipo == 'U'){
- graph[a].pb(mp(b,peso*norm));
- }
- }
- FloydWarshal();
- scanf("\n%c %c",&Davis,&Andre);
- pil rodovia = PegarRodoviaProxima(getVertex(Davis),getVertex(Andre));
- if(rodovia.second != oo){
- cout << rodovia.second << endl;
- printf("%c\n",getCharVertex(rodovia.first));
- }else{
- printf("-1\n");
- }
- return 0;
- }
- /*
- compilar
- g++ arquivo.cpp -std=c++11 -o executavel
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement