图论训练2C——hdu5137
来源:互联网 发布:怎么更改自己淘宝网址 编辑:程序博客网 时间:2024/05/16 11:58
C - How Many Maos Does the Guanxi Worth
Time Limit:1000MS Memory Limit:512000KB 64bit IO Format:%I64d & %I64uDescription
"Guanxi" is a very important word in Chinese. It kind of means "relationship" or "contact". Guanxi can be based on friendship, but also can be built on money. So Chinese often say "I don't have one mao (0.1 RMB) guanxi with you." or "The guanxi between them is naked money guanxi." It is said that the Chinese society is a guanxi society, so you can see guanxi plays a very important role in many things.
Here is an example. In many cities in China, the government prohibit the middle school entrance examinations in order to relief studying burden of primary school students. Because there is no clear and strict standard of entrance, someone may make their children enter good middle schools through guanxis. Boss Liu wants to send his kid to a middle school by guanxi this year. So he find out his guanxi net. Boss Liu's guanxi net consists of N people including Boss Liu and the schoolmaster. In this net, two persons who has a guanxi between them can help each other. Because Boss Liu is a big money(In Chinese English, A "big money" means one who has a lot of money) and has little friends, his guanxi net is a naked money guanxi net -- it means that if there is a guanxi between A and B and A helps B, A must get paid. Through his guanxi net, Boss Liu may ask A to help him, then A may ask B for help, and then B may ask C for help ...... If the request finally reaches the schoolmaster, Boss Liu's kid will be accepted by the middle school. Of course, all helpers including the schoolmaster are paid by Boss Liu.
You hate Boss Liu and you want to undermine Boss Liu's plan. All you can do is to persuade ONE person in Boss Liu's guanxi net to reject any request. This person can be any one, but can't be Boss Liu or the schoolmaster. If you can't make Boss Liu fail, you want Boss Liu to spend as much money as possible. You should figure out that after you have done your best, how much at least must Boss Liu spend to get what he wants. Please note that if you do nothing, Boss Liu will definitely succeed.
Here is an example. In many cities in China, the government prohibit the middle school entrance examinations in order to relief studying burden of primary school students. Because there is no clear and strict standard of entrance, someone may make their children enter good middle schools through guanxis. Boss Liu wants to send his kid to a middle school by guanxi this year. So he find out his guanxi net. Boss Liu's guanxi net consists of N people including Boss Liu and the schoolmaster. In this net, two persons who has a guanxi between them can help each other. Because Boss Liu is a big money(In Chinese English, A "big money" means one who has a lot of money) and has little friends, his guanxi net is a naked money guanxi net -- it means that if there is a guanxi between A and B and A helps B, A must get paid. Through his guanxi net, Boss Liu may ask A to help him, then A may ask B for help, and then B may ask C for help ...... If the request finally reaches the schoolmaster, Boss Liu's kid will be accepted by the middle school. Of course, all helpers including the schoolmaster are paid by Boss Liu.
You hate Boss Liu and you want to undermine Boss Liu's plan. All you can do is to persuade ONE person in Boss Liu's guanxi net to reject any request. This person can be any one, but can't be Boss Liu or the schoolmaster. If you can't make Boss Liu fail, you want Boss Liu to spend as much money as possible. You should figure out that after you have done your best, how much at least must Boss Liu spend to get what he wants. Please note that if you do nothing, Boss Liu will definitely succeed.
Input
There are several test cases.
For each test case:
The first line contains two integers N and M. N means that there are N people in Boss Liu's guanxi net. They are numbered from 1 to N. Boss Liu is No. 1 and the schoolmaster is No. N. M means that there are M guanxis in Boss Liu's guanxi net. (3 <=N <= 30, 3 <= M <= 1000)
Then M lines follow. Each line contains three integers A, B and C, meaning that there is a guanxi between A and B, and if A asks B or B asks A for help, the helper will be paid C RMB by Boss Liu.
The input ends with N = 0 and M = 0.
It's guaranteed that Boss Liu's request can reach the schoolmaster if you do not try to undermine his plan.
For each test case:
The first line contains two integers N and M. N means that there are N people in Boss Liu's guanxi net. They are numbered from 1 to N. Boss Liu is No. 1 and the schoolmaster is No. N. M means that there are M guanxis in Boss Liu's guanxi net. (3 <=N <= 30, 3 <= M <= 1000)
Then M lines follow. Each line contains three integers A, B and C, meaning that there is a guanxi between A and B, and if A asks B or B asks A for help, the helper will be paid C RMB by Boss Liu.
The input ends with N = 0 and M = 0.
It's guaranteed that Boss Liu's request can reach the schoolmaster if you do not try to undermine his plan.
Output
For each test case, output the minimum money Boss Liu has to spend after you have done your best. If Boss Liu will fail to send his kid to the middle school, print "Inf" instead.
Sample Input
4 51 2 31 3 71 4 502 3 43 4 23 21 2 302 3 100 0
Sample Output
50Inf有一个学生家长想通过关系找到校长让自己的孩子上学,找关系就要花钱,所有关系花的钱都有家长出,现在你派我们去破坏他的行为,我们去说服家长关系网络中一个人,让他在家长关系网络中不起作用,从而达到让家长不能找到校长的目的;如果没有这样一个存在,那我们就让家长花费最多;家长肯定是要花费越少越好;如果能阻止家长通过关系找到校长,输出Inf,否者,输出家长需要的花费;题目关系点最多30个,1表示家长n表示校长,我们可以遍历2~n-1,删除这些关系点,然后寻找最短,求最短路的最大值,我是在建立边时,给边加了一个状态值,求最短路时,在状态允许的情况求最短路,最短路用的dijkstra加的优先队列优化#include <iostream>#include <stdio.h>#include <algorithm>#include <queue>#include <string.h>#include <math.h>using namespace std;int n,m;const int inf=0x3f3f3f3f,maxn=2010;struct Edge{ int u,v,w,next; bool ok;} edge[maxn];int dis[50];struct P{ int u,v; P() { ; } P(int x,int y) { u=x; v=y; } friend bool operator<(const P a,const P b) { return a.u>b.u; }};int tot;int head[maxn];bool vis[maxn];void init(int n){ tot=0; memset(head,-1,sizeof(head));}void addEdge(int u,int v,int w){ edge[tot].v=v; edge[tot].w=w; edge[tot].ok=true; edge[tot].next=head[u]; head[u]=tot++;}int dijkstra(int s,int t){ priority_queue<P>que; for(int i=1; i<=n; i++) dis[i]=inf; dis[s]=0; que.push(P(0,s)); while(!que.empty()) { P p=que.top(); que.pop(); int u=p.v; if(dis[u]<p.u) continue; for(int i=head[u]; i!=-1; i=edge[i].next) { if(!edge[i].ok) continue; int v=edge[i].v; if(dis[v]>dis[u]+edge[i].w) { dis[v]=dis[u]+edge[i].w; que.push(P(dis[v],v)); } } } return dis[t];}int main(){ int u,v,w; while(scanf("%d%d",&n,&m)!=-1) { init(n); if(n==0&&m==0) break; for(int i=0; i<m; i++) { scanf("%d%d%d",&u,&v,&w); addEdge(u,v,w); addEdge(v,u,w); } int ans=0; for(int i=2; i<=n-1; i++) { for(int j=head[i]; j!=-1; j=edge[j].next)///由i顶点出发的以及进入的边都标记不可用 edge[j].ok=edge[j^1].ok=false; ans=max(ans,dijkstra(1,n)); for(int j=head[i]; j!=-1; j=edge[j].next) edge[j].ok=edge[j^1].ok=true; } if(ans>=inf) printf("Inf\n"); else printf("%d\n",ans); } return 0;}
0 0
- 图论训练2C——hdu5137
- HDU5137
- hdu5137
- 简单数学训练—C
- TJU训练赛—C
- c语言练习题——字符串训练
- 递推递归训练——C
- 第九周训练赛——C
- poj1062——图论训练2B
- C语言算法训练2
- dp训练2C---poj1185
- c语言强化训练——简易计算器
- c语言练习题——整数算法训练
- c语言练习题——递归和栈编程训练
- c语言练习题——指针和链表训练
- C语言学习历程——Training04字符串训练01
- C语言学习历程——Training04字符串训练02
- C语言学习历程——Training04字符串训练03
- 【LightOJ】1138 - Trailing Zeroes (III)(数论,二分法)(POJ-1401类型题)
- Android Studio多工程引用同一个library项目配置方法
- Realsense(一)提取彩色和深度视频流并实现pxcimage到mat
- iOS开发中设置UITableViewCell选中时的颜色
- spring + mybatis 创建项目
- 图论训练2C——hdu5137
- 最适合程序员转行的10大职业
- hdu 2087 剪花布条 求模式串在主串中的个数
- 经典排序算法 - 冒泡排序Bubble sort
- Android访问网络,使用HttpURLConnection还是HttpClient?
- iOS状态栏颜色的修改
- BZOJ 1190: [HNOI2007]梦幻岛宝珠
- POJ 3080 Blue Jeans
- You must supply a resourceID for a TextView