标签:class clu cap sam from lin ane others cep
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5832 Accepted Submission(s): 1493
代码如下:
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 }
HDU2121 Ice_cream’s world II —— 最小树形图 + 超级点
标签:class clu cap sam from lin ane others cep
原文地址:http://www.cnblogs.com/DOLFAMINGO/p/7768046.html