题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121
Ice_cream’s world II
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5832 Accepted Submission(s): 1493
Problem Description
After awarded lands to ACMers, the queen want to choose a city be her capital. This is an important event in ice_cream world, and it also a very difficult problem, because the world have N cities and M roads, every road was directed. Wiskey is a chief engineer in ice_cream world. The queen asked Wiskey must find a suitable location to establish the capital, beautify the roads which let capital can visit each city and the project’s cost as less as better. If Wiskey can’t fulfill the queen’s require, he will be punishing.
Input
Every case have two integers N and M (N<=1000, M<=10000), the cities numbered 0…N-1, following M lines, each line contain three integers S, T and C, meaning from S to T have a road will cost C.
Output
If no location satisfy the queen’s require, you must be output “impossible”, otherwise, print the minimum cost in this project and suitable city’s number. May be exist many suitable cities, choose the minimum number city. After every case print one blank.
Sample Input
3 1
0 1 1
4 4
0 1 10
0 2 10
1 3 20
2 3 30
Sample Output
impossible
40 0
Author
Wiskey
Source
HDU 2007-10 Programming Contest_WarmUp
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 using namespace std; 7 typedef long long LL; 8 const double EPS = 1e-6; 9 const int INF = INT_MAX; 10 const LL LNF = 9e18; 11 const int MOD = 1e9+7; 12 const int MAXN = 1e3+10; 13 14 struct Edge 15 { 16 int u, v, w; 17 }edge[10010]; 18 19 int super_edge, root_pos; 20 int pre[MAXN], id[MAXN], vis[MAXN], in[MAXN]; 21 22 int zhuliu(int root, int n, int m) 23 { 24 int res = 0; 25 while(1) 26 { 27 for(int i = 0; i<n; i++) 28 in[i] = INF; 29 for(int i = 0; i<m; i++) 30 if(edge[i].u!=edge[i].v && edge[i].w<in[edge[i].v]) 31 { 32 pre[edge[i].v] = edge[i].u; 33 in[edge[i].v] = edge[i].w; 34 if(edge[i].u==root) 35 root_pos = i; 36 } 37 38 for(int i = 0; i<n; i++) 39 if(i!=root && in[i]==INF) 40 return -1; 41 42 int tn = 0; 43 memset(id, -1, sizeof(id)); 44 memset(vis, -1, sizeof(vis)); 45 in[root] = 0; 46 for(int i = 0; i<n; i++) 47 { 48 res += in[i]; 49 int v = i; 50 while(vis[v]!=i && id[v]==-1 && v!=root) 51 { 52 vis[v] = i; 53 v = pre[v]; 54 } 55 if(v!=root && id[v]==-1) 56 { 57 for(int u = pre[v]; u!=v; u = pre[u]) 58 id[u] = tn; 59 id[v] = tn++; 60 } 61 } 62 if(tn==0) break; 63 for(int i = 0; i<n; i++) 64 if(id[i]==-1) 65 id[i] = tn++; 66 67 for(int i = 0; i<m; i++) 68 { 69 int v = edge[i].v; 70 edge[i].u = id[edge[i].u]; 71 edge[i].v = id[edge[i].v]; 72 if(edge[i].u!=edge[i].v) 73 edge[i].w -= in[v]; 74 } 75 n = tn; 76 root = id[root]; 77 } 78 return res; 79 } 80 81 int main() 82 { 83 int n, m; 84 while(scanf("%d%d", &n, &m)!=EOF) 85 { 86 super_edge = 0; 87 for(int i = 0; i<m; i++) 88 { 89 scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].w); 90 super_edge += edge[i].w; 91 } 92 93 super_edge++; 94 for(int i = 0; i<n; i++) 95 { 96 edge[m+i].u = n; 97 edge[m+i].v = i; 98 edge[m+i].w = super_edge; 99 } 100 101 int ans = zhuliu(n, n+1, m+n); 102 if(ans==-1 || ans>=2*super_edge) printf("impossible\n\n"); 103 else printf("%d %d\n\n", ans-super_edge, root_pos-m); 104 } 105 }