http://lightoj.com/volume_showproblem.php?problem=1074
Time Limit: 2 second(s) | Memory Limit: 32 MB |
Dhaka city is getting crowded and noisy day by day. Certain roads always remain blocked in congestion. In order to convince people avoid shortest routes, and hence the crowded roads, to reach destination, the city authority has made a new plan. Each junction of the city is marked with a positive integer (≤ 20) denoting the busyness of the junction. Whenever someone goes from one junction (the source junction) to another (the destination junction), the city authority gets the amount (busyness of destination - busyness of source)3 (that means the cube of the difference) from the traveler. The authority has appointed you to find out the minimum total amount that can be earned when someone intelligent goes from a certain junction (the zero point) to several others.
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
Each case contains a blank line and an integer n (1 < n ≤ 200) denoting the number of junctions. The next line contains n integers denoting the busyness of the junctions from 1 to n respectively. The next line contains an integer m, the number of roads in the city. Each of the next m lines (one for each road) contains two junction-numbers (source, destination) that the corresponding road connects (all roads are unidirectional). The next line contains the integer q, the number of queries. The next q lines each contain a destination junction-number. There can be at most one direct road from a junction to another junction.
Output
For each case, print the case number in a single line. Then print q lines, one for each query, each containing the minimum total earning when one travels from junction 1 (the zero point) to the given junction. However, for the queries that gives total earning less than 3, or if the destination is not reachable from the zero point, then print a '?'.
Sample Input |
Output for Sample Input |
2 5 6 7 8 9 10 6 1 2 2 3 3 4 1 5 5 4 4 5 2 4 5 2 10 10 1 1 2 1 2 |
Case 1: 3 4 Case 2: ? |
SPFA 判断负环,标记负环可达的。
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-10-2 18:08:34 4 File Name :E:2013ACM练习专题强化训练图论一LightOJ1074.cpp 5 ************************************************ */ 6 7 #include <stdio.h> 8 #include <string.h> 9 #include <iostream> 10 #include <algorithm> 11 #include <vector> 12 #include <queue> 13 #include <set> 14 #include <map> 15 #include <string> 16 #include <math.h> 17 #include <stdlib.h> 18 #include <time.h> 19 using namespace std; 20 21 const int MAXN = 220; 22 const int INF = 0x3f3f3f3f; 23 struct Edge 24 { 25 int v,cost; 26 Edge(int _v = 0, int _cost = 0) 27 { 28 v = _v; 29 cost = _cost; 30 } 31 }; 32 vector<Edge>E[MAXN]; 33 void addedge(int u,int v,int w) 34 { 35 E[u].push_back(Edge(v,w)); 36 } 37 38 bool vis[MAXN]; 39 int cnt[MAXN]; 40 int dist[MAXN]; 41 42 bool cir[MAXN]; 43 void dfs(int u) 44 { 45 cir[u] = true; 46 for(int i = 0;i < E[u].size();i++) 47 if(!cir[E[u][i].v]) 48 dfs(E[u][i].v); 49 } 50 51 void SPFA(int start,int n) 52 { 53 memset(vis,false,sizeof(vis)); 54 for(int i = 1;i <= n;i++) 55 dist[i] = INF; 56 vis[start] = true; 57 dist[start] = 0; 58 queue<int>que; 59 while(!que.empty())que.pop(); 60 que.push(start); 61 memset(cnt,0,sizeof(cnt)); 62 cnt[start] = 1; 63 memset(cir,false,sizeof(cir)); 64 while(!que.empty()) 65 { 66 int u = que.front(); 67 que.pop(); 68 vis[u] = false; 69 for(int i = 0;i < E[u].size();i++) 70 { 71 int v = E[u][i].v; 72 if(cir[v])continue; 73 if(dist[v] > dist[u] + E[u][i].cost) 74 { 75 dist[v] = dist[u] + E[u][i].cost; 76 if(!vis[v]) 77 { 78 vis[v] = true; 79 que.push(v); 80 cnt[v]++; 81 if(cnt[v] > n) 82 dfs(v); 83 } 84 } 85 } 86 } 87 } 88 int a[MAXN]; 89 int main() 90 { 91 //freopen("in.txt","r",stdin); 92 //freopen("out.txt","w",stdout); 93 int T; 94 int iCase = 0; 95 scanf("%d",&T); 96 while(T--) 97 { 98 iCase++; 99 int n; 100 scanf("%d",&n); 101 for(int i = 1;i <= n;i++) 102 scanf("%d",&a[i]); 103 int m; 104 int u,v; 105 for(int i = 1;i <= n;i++) 106 E[i].clear(); 107 scanf("%d",&m); 108 while(m--) 109 { 110 scanf("%d%d",&u,&v); 111 addedge(u,v,(a[v]-a[u])*(a[v]-a[u])*(a[v]-a[u])); 112 } 113 SPFA(1,n); 114 printf("Case %d: ",iCase); 115 scanf("%d",&m); 116 while(m--) 117 { 118 scanf("%d",&u); 119 if(cir[u] || dist[u] < 3 || dist[u] == INF) 120 printf("? "); 121 else printf("%d ",dist[u]); 122 } 123 } 124 return 0; 125 }