Ice_cream’s world II - HDU 2121 朱刘算法
2015-06-04 10:21
344 查看
Ice_cream’s world II
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3398 Accepted Submission(s): 812
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
题意:求花费最小的最小树形图。
思路:在朱刘算法的基础上,建立一个虚点,让虚点指向所有的点,其权值为sum+1,然后用这个虚点去做朱刘算法的根节点。
AC代码如下:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; struct node { int u,v; ll w; }edge[20010]; int n,m,pre[1010],id[1010],vis[1010],INF=1e18,pos; ll in[1010]; ll Directed_MST(int root,int V,int E) { ll ret=0; int i,j,k,u,v,cnt; while(true) { for(i=0;i<V;i++) in[i]=INF; for(i=0;i<E;i++) { u=edge[i].u; v=edge[i].v; if(edge[i].w<in[v] && u!=v) { pre[v]=u; in[v]=edge[i].w; if(u==root) pos=i; } } for(i=0;i<V;i++) if(i!=root &&in[i]==INF) return -1; cnt=0; memset(id,-1,sizeof(id)); memset(vis,-1,sizeof(vis)); in[root]=0; for(i=0;i<V;i++) { ret+=in[i]; v=i; while(vis[v]!=i && id[v]==-1 && v!=root) { vis[v]=i; v=pre[v]; } if(v!=root && id[v]==-1) { for(u=pre[v];u!=v;u=pre[u]) id[u]=cnt; id[v]=cnt++; } } if(cnt==0) break; for(i=0;i<V;i++) if(id[i]==-1) id[i]=cnt++; for(i=0;i<E;i++) { u=edge[i].u; v=edge[i].v; edge[i].u=id[u]; edge[i].v=id[v]; if(id[u]!=id[v]) edge[i].w-=in[v]; } V=cnt; root=id[root]; } return ret; } int main() { int i,j,k,u,v; ll ans,sum; while(~scanf("%d%d",&n,&m)) { sum=1; for(i=0;i<m;i++) { scanf("%d%d%I64d",&edge[i].u,&edge[i].v,&edge[i].w); edge[i].u++;edge[i].v++; sum+=edge[i].w; } for(i=1;i<=n;i++) { edge[m+i-1].u=0;edge[m+i-1].v=i;edge[m+i-1].w=sum; } ans=Directed_MST(0,n+1,n+m); if(ans==-1 || ans>=2*sum) printf("impossible\n\n"); else printf("%I64d %d\n\n",ans-sum,pos-m); } }
相关文章推荐
- Linux 命令笔记(1)
- php 函数实参的处理时 数组和对象的不同
- 17个新手常见的运行时错误
- Python通过select实现异步IO的方法
- 简单工厂模式---商场收费系统
- 网站扫描工具paros proxy(v3.2.13)安装与使用
- eclipse 配置JDK
- Python模拟RSA算法
- 美团网App中显示项界面设计
- c++ 基础语法一(空类型的sizeof)
- 《编写可读代码的艺术》读后总结
- mysql的data数据库只有.frm 而.myd和.myi没有,使用正常
- ios 正则获取img 里的 src
- Command Network - POJ 3164 朱刘算法
- RSA和AES在c#服务端和android客户端之间的加密解密
- 子集和
- 关于鼠标滚轴事件的检测问题
- linux程序分析工具介绍(二)—-ldd,nm
- LeetCode OJ 之 Median of Two Sorted Arrays(两个有序数组的中位数)
- angularjs学习:$digest