您的位置:首页 > 其它

HDU 2121 Ice_cream’s world II

2017-06-03 09:04 281 查看
Ice_cream’s world II
Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4842    Accepted Submission(s): 1183


[align=left]Problem Description[/align]
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.
 
[align=left]Input[/align]
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.
 
[align=left]Output[/align]
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.
 
[align=left]Sample Input[/align]

3 1
0 1 1
 
4 4

0 1 10
0 2 10
1 3 20
2 3 30

 
[align=left]Sample Output[/align]

impossible
 
40 0
 

1 #include <iostream>
2 #include <cstring>
3 #define N 1005
4 #define inf 0x7f7f7f7f
5 using namespace std;
6 struct node{
7     long long int w;
8     int u,v;
9 }edge[N*N];
10 int pre
,id
,n,m,pos,vis
;
11 long long int in
;
12 long long int solve(int root,int v,int e){
13     long long int sum=0;
14     while(true){
15         //1.找每个节点的最小入边
16         for(int i=0;i<v;i++){
17             in[i]=inf;
18         }
19         for(int i=0;i<e;i++){
20             int a=edge[i].u;
21             int b=edge[i].v;
22             if(edge[i].w<in[b]&&a!=b){
23                 pre[b]=a;
24                 in[b]=edge[i].w;
25                 if(a==root){
26                     pos=i;
27                 }
28             }
29         }
30         // for(int i=0;i<v;i++){//本题不用,因为全部点都有虚拟节点到该点的一条路径
31         //     if(i==root){
32         //         continue;
33         //     }
34         //     if(in[i]==inf){
35         //         return -1;
36         //     }
37         // }
38         //2.找环
39         int cnt = 0;
40         memset(id,-1,sizeof(id));
41         memset(vis,-1,sizeof(vis));
42         in[root] = 0;
43         for(int i=0;i<v;i++){
44             sum+=in[i];
45             int k=i;
46             while(vis[k]!=i&&id[k]==-1&&k!=root){
47                 vis[k]=i;
48                 k=pre[k];
49             }
50             if(k!=root&&id[k]==-1){
51                 for(int j=pre[k];j!=k;j=pre[j]){
52                     id[j]=cnt;//标记节点u为第几个环
53                 }
54                 id[k]=cnt++;
55             }
56         }
57         if(cnt==0){
58             break;
59         }
60         //3.建立新图   缩点,重新标记
61         for(int i=0;i<v;i++){
62             if(id[i]==-1){
63                 id[i]=cnt++;
64             }
65         }
66         for(int i=0;i<e;i++){
67             int a=edge[i].u;
68             int b=edge[i].v;
69             edge[i].u=id[a];
70             edge[i].v=id[b];
71             if(id[a]!=id[b]){
72                 edge[i].w-=in[b];
73             }
74         }
75         v=cnt;
76         root=id[root];
77     }
78     return sum;
79 }
80 int main(){
81     cin.sync_with_stdio(false);
82     long long int sum;
83     while(cin>>n>>m){
84         sum=0;
85         for(int i=0;i<m;i++){
86             cin>>edge[i].u>>edge[i].v>>edge[i].w;
87             edge[i].u++;
88             edge[i].v++;
89             sum+=edge[i].w;
90         }
91         sum++;
92         for(int i=m;i<m+n;i++){//增加超级节点0,节点0到其余各个节点的边权相同(此题中 边权要大于原图的总边权值)
93             edge[i].u=0;
94             edge[i].v=i-m+1;
95             edge[i].w=sum;
96         }
97         long long int ans=solve(0,n+1,n+m);
98         //n+1为总结点数,m+n为总边数
99         //ans代表以超级节点0为根的最小树形图的总权值,
100         //将ans减去sum,如果差值小于sum,说明节点0的出度只有1(最大的是全部给的边(sum-1)都要再加上sum),说明原图是连通图
101         //如果差值>=sum,那么说明节点0的出度不止为1,说明原图不是连通图
102         if(ans==-1||ans-sum>=sum){
103             cout<<"impossible"<<endl;
104         }
105         else{
106             cout<<(ans-sum)<<" "<<(pos-m)<<endl;//pos为虚拟节点的下标,实际为pos-m
107         }
108         cout<<endl;
109     }
110     return 0;
111 }


2016-12-17 15:26:48

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: