HDU5137 How Many Maos Does the Guanxi Worth(最短路,spfa)

来源:互联网 发布:starstail mac 安装 编辑:程序博客网 时间:2024/05/21 18:38

题目:

How Many Maos Does the Guanxi Worth

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 2618    Accepted Submission(s): 1019


Problem Description
"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.
 

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.
 

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
 

Source
2014ACM/ICPC亚洲区广州站-重现赛(感谢华工和北大)
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6022 6021 6020 6019 6018 
 

Statistic | Submit | Discuss | Note
思路:

给了n个点m条边然后给出了各个边的权值,然后想要从第一个点走到最后一个点,你可以使某一个点失效,增加刘老板从第一个点走到最后一个点的难度,然后在求最短路,如果走不到输出Inf,因为顶点数不多,所以可以用邻接矩阵建图,然后在把每一个点spfa一遍,从里面找出最大的就好

代码:

#include <cstdio>#include <cstring>#include <cctype>#include <string>#include <set>#include <iostream>#include <stack>#include <cmath>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 10000007#define debug() puts("what the fuck!!!")#define N 1200#define M 1000000#define ll long longusing namespace std;int map[50][50];int vis[50];int dis[50];int n,m;void spfa(int p){    queue<int>q;    mem(vis,0);    for(int i=1; i<=n; i++)        dis[i]=inf;    vis[1]=1;    dis[1]=0;    q.push(1);    while(!q.empty())    {        int k=q.front();        vis[k]=0;        q.pop();        for(int i=1; i<=n; i++)        {            int w=map[k][i];            if(i==p||k==p)                w=inf;            if(dis[k]+w<dis[i])            {                dis[i]=dis[k]+w;                if(!vis[i])                {                    vis[i]=1;                    q.push(i);                }            }        }    }}int main(){    int a,b,c;    while(scanf("%d%d",&n,&m)&&n&&m)    {        mem(map,inf);        for(int i=1; i<=m; i++)        {            scanf("%d%d%d",&a,&b,&c);            if(map[a][b]>c)                map[a][b]=c;            if(map[b][a]>c)                map[b][a]=c;        }        int maxx=0;        for(int i=2; i<n; i++)        {            spfa(i);            maxx=max(maxx,dis[n]);        }        if(maxx==inf)            puts("Inf");        else            printf("%d\n",maxx);    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 高中孩子班管理松怎么办 脱式计算有余数怎么办 手机被别人绑定微信怎么办 饿了么入职查不到学历怎么办 高中没考上家人让打工怎么办 一建证书丢了怎么办 自考大专证书丢了怎么办 护士学分卡丢了怎么办 公司电脑只能用内网怎么办 遇见素质低的人怎么办 被素质低的人骂怎么办 对素质差的人怎么办 碰到素质低的人怎么办 和韩国人结婚后工作怎么办 大连明珠卡坏了怎么办 护肤品酒精含量太高怎么办 父母出国三年想孩子怎么办 孩子高考不理想想出国怎么办 交大附中创新班剩余孩子怎么办 棋牌开发公司倒闭了怎么办 7月1日本地流量怎么办 装电池的弹簧生锈了怎么办 电器被电池碱了怎么办 车辆被交警拖走怎么办 香烟熏的眼睛疼怎么办 想在中牟住5天怎么办 腰弯了直不了怎么办 自考科目停考了怎么办 把wlan密码忘了怎么办 滕州不给报新婚怎么办 体内锁水能力差怎么办 别人说你人品差怎么办 面对没素质的人怎么办 遇到很坏的室友怎么办 餐饮员工与顾客发生矛盾怎么办 新员工老员工出现矛盾怎么办 手机移动4g网慢怎么办 碰到素质低的老公怎么办 小孩做事拖拉 没时间观念怎么办 execl表中日期加3.5怎么办 狗狗喜欢咬人怎么办