Ice_cream’s world II
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3005 Accepted Submission(s): 704
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
题目大意:一个国家有N个城市,M条有向道路,国王想要选一个城市为首都,
使得这个城市既能连接所有的城市,而且总的路程最短。若能找到这个城市,则
输出最短路程和城市编号。
思路:求有向图的最小树形图,不过根节点是不定的。虚拟一个根结点,到每个
结点都有一条边,其到每个结点的权值应该相等,表明每个结点都应该且都有机
会当根。且边的权值应比总权值大一些。如果最终算出的最小树形图的总权值大
于或等于 原图的总权值 + 虚根到实根(也就是原图的总权值),其实就是算出的最
小树形图的总权值大于或等于 原图总权值的二倍,就不能构成有向图的最小树形
图。
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #include<climits> using namespace std; const int MAXN = 1100; const int MAXM = 11010; struct Node { int from; int to; int w; }Edges[MAXM]; int pre[MAXN],vis[MAXN],flag[MAXN],In[MAXN],sum,pos; int ZhuLiu(int root,int N,int M) { sum = 0; while(true) { for(int i = 0; i < N; ++i) In[i] = INT_MAX; for(int i = 0; i < M; ++i) { int u = Edges[i].from; int v = Edges[i].to; if(Edges[i].w < In[v] && u != v) { pre[v] = u; In[v] = Edges[i].w; if(u == root) pos = i; } } for(int i = 0; i < N; ++i) { if(i == root) continue; if(In[i] == INT_MAX) return -1; } int CntNode = 0; memset(flag,-1,sizeof(flag)); memset(vis,-1,sizeof(vis)); In[root] = 0; for(int i = 0; i < N; ++i) { sum += In[i]; int v = i; while(vis[v] != i && flag[v] == -1 && v != root) { vis[v] = i; v = pre[v]; } if(v != root && flag[v] == -1) { for(int u = pre[v]; u != v; u = pre[u]) flag[u] = CntNode; flag[v] = CntNode++; } } if(CntNode == 0) break; for(int i = 0; i < N; ++i) { if(flag[i] == -1) flag[i] = CntNode++; } for(int i = 0; i < M; ++i) { int v = Edges[i].to; Edges[i].from = flag[Edges[i].from]; Edges[i].to = flag[Edges[i].to]; if(Edges[i].from != Edges[i].to) Edges[i].w -= In[v]; } N = CntNode; root = flag[root]; } return sum; } int main() { int N,M; while(~scanf("%d%d",&N,&M)) { int Sum = 0; for(int i = 0; i < M; ++i) { scanf("%d%d%d",&Edges[i].from,&Edges[i].to,&Edges[i].w); Sum += Edges[i].w; } Sum++; for(int i = 0; i < N; ++i) { Edges[M+i].from = N; Edges[M+i].to = i; Edges[M+i].w = Sum; } int ans = ZhuLiu(N,N+1,M+N); if(ans >= 2*Sum) printf("impossible\n\n"); else printf("%d %d\n\n",ans - Sum, pos - M); } return 0; }