Advertisement
Guest User

uva 10449

a guest
Sep 20th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. ///***Bismillahir Rahmanir Rahim***
  2. ///***Faizul***
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. typedef long long ll;
  6. #define INF 1e9+7
  7. struct edge
  8. {
  9.     ll U;ll V;ll W;
  10.     edge(ll a,ll b,ll c)
  11.     {
  12.         U=a;
  13.         V=b; W=c;
  14.     }
  15. };
  16.  
  17. vector <edge> graph;
  18.  
  19. ll busyness[5000];
  20.  
  21. ll dist[5000];
  22.  
  23. ll node,E;
  24.  
  25. void bellmanford(ll source)
  26. {
  27.     for(ll i=0;i<5000;i++) dist[i]=INF;
  28.  
  29.     //for(int i=1;i<=node;i++) cout<<dist[i]<<endl;
  30.  
  31.     dist[source]=0;
  32.  
  33.     for(ll i=1;i<10000;i++)
  34.     {
  35.         for(ll j=0;j<E;j++)
  36.         {
  37.             ll u=graph[j].U;
  38.             ll v=graph[j].V;
  39.             ll cost=graph[j].W;
  40.  
  41.             if(dist[u]+cost <dist[v] && dist[u]!=INF) dist[v]=dist[u]+cost;
  42.         }
  43.     }
  44.  
  45.  
  46. }
  47.  
  48. int main()
  49. {
  50.  
  51.     //freopen("1.txt","r",stdin);
  52.     //freopen("2.txt","w",stdout);
  53.  
  54.     ll test=1;
  55.     ll N,M,Q;
  56.  
  57.     while(cin>>N)
  58.     {
  59.         for(ll j=1;j<=N;j++)
  60.         {
  61.             cin>>busyness[j];
  62.             //scanf("%lld",&busyness[j]);
  63.         }
  64.  
  65.         cin>>M;
  66.         //scanf("%lld",&M);
  67.  
  68.         int u,v;
  69.         for(ll j=1;j<=M;j++)
  70.         {
  71.             cin>>u>>v;
  72.             //scanf("%lld %lld",&u,&v);
  73.  
  74.             ll w=busyness[v]-busyness[u];
  75.             w=w*w*w;
  76.  
  77.             graph.push_back(edge(u,v,w));
  78.         }
  79.  
  80.         node=N; E=M;
  81.  
  82.         cin>>Q;
  83.         //scanf("%lld",&Q);
  84.  
  85.         bellmanford(1);
  86.  
  87.         ll source;
  88.  
  89.         ll ans[10000];
  90.  
  91.         //for(int j=1;j<=N;j++) cout<<dist[j]<<endl;
  92.  
  93.         for(ll j=1;j<=Q;j++)
  94.         {
  95.             cin>>source;
  96.             //scanf("%lld",&source);
  97.  
  98.             ll X=dist[source];
  99.             //cout<<"query "<<source <<" "<<X<<endl;
  100.  
  101.             if(X<3 || X==INF) ans[j]=-1;
  102.  
  103.             else ans[j]=X;
  104.         }
  105.  
  106.         printf("Set #%lld\n",test);
  107.         test++;
  108.  
  109.         for(int j=1;j<=Q;j++)
  110.         {
  111.             if(ans[j]==-1) printf("?\n");
  112.             else printf("%lld\n",ans[j]);
  113.         }
  114.  
  115.         memset(dist,0,sizeof(dist));
  116.         memset(busyness,0,sizeof(busyness));
  117.         graph.clear();
  118.  
  119.     }
  120.  
  121.     return 0;
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement