hdu 2121 Ice_cream’s world II(最小树形图,不定根)
2014-08-19 14:51
417 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121
[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
[align=left]Author[/align]
Wiskey
[align=left]Source[/align]
HDU 2007-10 Programming Contest_WarmUp
参考大神的思路:
本题为不是固定根的最小树形图,我们可以虚拟出一根来,然后在把这个根跟每个点相连,相连的点可以设为无穷大,或者设为所有边和大一点,比如为r,然后就可以利用最小树形图进行计算了,计算出的结果减去r,如果比r还大就可以认为通过这个虚拟节点我们连过原图中两个点,即原图是不连通的,我们就可以认为不存在最小树形图。关于输出最小根也挺简单,在找最小入弧如果这条弧的起点是虚拟根,那么这条弧的终点就是要求的根。
代码:
[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
[align=left]Author[/align]
Wiskey
[align=left]Source[/align]
HDU 2007-10 Programming Contest_WarmUp
参考大神的思路:
本题为不是固定根的最小树形图,我们可以虚拟出一根来,然后在把这个根跟每个点相连,相连的点可以设为无穷大,或者设为所有边和大一点,比如为r,然后就可以利用最小树形图进行计算了,计算出的结果减去r,如果比r还大就可以认为通过这个虚拟节点我们连过原图中两个点,即原图是不连通的,我们就可以认为不存在最小树形图。关于输出最小根也挺简单,在找最小入弧如果这条弧的起点是虚拟根,那么这条弧的终点就是要求的根。
代码:
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<string> #include<math.h> #include<stdlib.h> #include<map> #include<stack> #include<queue> #include<set> #include<vector> #define max(x,y)x>y?x:y #define min(x,y)x<y?x:y #define swap(a,b) a=a^b,b=a^b,a=a^b using namespace std; const int maxn=1005; const int INF=1<<30; typedef struct { int u,v; int cost; }node; node edge[maxn*maxn]; int pre[maxn],id[maxn],vis[maxn]; int in[maxn]; int n,m; int res; int mst(int root,int nv,int ne) { int ans=0; while(1) { //1.寻找最小入度边 for(int i=0;i<nv;i++) in[i]=INF; for(int i=0;i<ne;i++) { int u=edge[i].u; int v=edge[i].v; if(edge[i].cost<in[v]&&u!=v) { pre[v]=u; if(u==root) //记录root是从哪一条边到有效点的,这个点就是实际的起点 res=i; in[v]=edge[i].cost; } } for(int i=0;i<nv;i++) { if(i==root) continue; if(in[i]==INF) return -1; } //2.找环 int cnt=0; memset(id,-1,sizeof(id)); memset(vis,-1,sizeof(vis)); in[root]=0; //根节点入度为0 for(int i=0;i<nv;i++)//标记每个环 { ans+=in[i]; int v=i; while(vis[v]!=i&&id[v]==-1&&v!=root) { vis[v]=i; v=pre[v]; } if(v!=root&&id[v]==-1) { for(int u=pre[v];u!=v;u=pre[u]) id[u]=cnt; id[v]=cnt++; } } if(cnt==0) break;//无环 for(int i=0;i<nv;i++) if(id[i]==-1) id[i]=cnt++; //3.缩点,重新标记 for(int i=0;i<ne;i++) { int v=edge[i].v; edge[i].u=id[edge[i].u]; edge[i].v=id[edge[i].v]; if(edge[i].u!=edge[i].v) edge[i].cost-=in[v]; } nv=cnt; root=id[root]; } return ans; } int main() { //freopen("in.txt","r",stdin); int s,t,c; int sum,cost; while(scanf("%d %d",&n,&m)==2) { sum=0; for(int i=0;i<m;i++) { scanf("%d %d %d",&s,&t,&c); s++; t++; edge[i].u=s; edge[i].v=t; edge[i].cost=c; sum+=c; } sum++; for(int i=m;i<m+n;i++) //虚拟出来的那个节点 { edge[i].u=0; edge[i].v=i-m+1; edge[i].cost=sum; } int ans=mst(0,n+1,m+n); if(ans==-1||ans-sum>=sum) printf("impossible\n"); else printf("%d %d\n",ans-sum,res-m); printf("\n"); } return 0; }
相关文章推荐
- hdu 2121 Ice_cream’s world II(不定根的最小树形图)
- HDU 2121——Ice_cream’s world II——————【最小树形图、不定根】
- 不定根最小树形图 Ice_cream’s world II HDU - 2121
- HDU 2121 Ice_cream’s world II 不定根最小树形图
- HDU 2121 Ice_cream’s world II 不定根最小树形图
- HDU 2121 Ice_cream’s world II 不定根最小树形图
- HDU 2121 Ice_cream’s world II 无固定点的最小树形图 朱刘算法
- HDU 2121 Ice_cream’s world II 最小树形图
- HDU 2121 Ice_cream’s world II(无固定根最小树形图、找最小根)
- HDU ACM 2121 Ice_cream’s world II (无根最小树形图)
- HDU 2121 Ice_cream’s world II(无定根最小树形图)
- HDU 2121 Ice_Cream's World II (最小树形图)
- [HDU 2121] Ice_cream’s world II 最小树形图
- HDU 2121 Ice_cream’s world II(最小树形图)
- hdu 2121 Ice_cream’s world II(无固定根最小树形图)
- HDUOJ 2121 Ice_cream’s world II(不定根的最小树形图-好题)
- hdu 2121 Ice_cream’s world II(无根结点最小树形图)
- [HDU 2121]Ice_cream’s world II[无定根的最小树形图]
- HDU 2121 Ice_cream’s world II 无固定根的最小树形图以及最小根
- HDU 2121 Ice_cream’s world II 最小树形图 模板