HDU - 5137

"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 5
1 2 3
1 3 7
1 4 50
2 3 4
3 4 2
3 2
1 2 30
2 3 10
0 0
Sample Output
50
Inf

这道题,只想说,注意题意的表达。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
/*
这道题,我想说一下,我的看法,难点
1.就是选边,两条相同的边,我们选长的还是选择短的,或者说我们能决定么,这个选择人是大款,他为了少花钱,肯定
 选择少的,至于题目里说的要花最多的钱,是在我们封锁人的情况下,但是那些边不是我们能力所能及的,我们只能删掉
 一个人,其余的什么都不能干,所以对于同起同终的边,我们选择的是小的。
 2.就是枚举的这种思路,我们之前想的就是记录下来最小的路径上面有谁,然后看看谁能在最小的那几条路径上,可
 事实就是我们根本没有办法来实现,我们只能采用枚举的方式,因为点毕竟只有30,这就是很像那个外星人的题
 我们只需要枚举出来点,最后通过筛选,来确定最后的答案,也就是暴力的思想,不能每次都是说可以直接从条件
 直接跨越到答案,有些题,我们看一下范围,然后就可以采用枚举的方式,通过比较,最后选出,最佳的方案
*/

int n,m;
int mp[32][32];//关系图
int vis[32];
int dis[32];

int dijkstra(int del)
{
    memset(vis,0,sizeof(vis));
    memset(dis,INF,sizeof(dis));
    vis[del]=1;//也就是说这个人的点已经被访问,不能当做中继点,也不能当做接收点
    dis[1]=0;
    for(int i=1;i<=n;i++)
    {
        int temp=INF,m;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&dis[j]<temp)
            {
                temp=dis[j];
                m=j;//找到中间点0
            }
        }
        vis[m]=1;
        for(int j=1;j<=n;j++)
        {
            if(j!=del&&dis[j]>dis[m]+mp[m][j])
                dis[j]=dis[m]+mp[m][j];
        }
    }
    return dis[n];
}


int main()
{
    while(~scanf("%d %d",&n,&m))
    {
       if(!n&&!m) break;
       memset(mp,INF,sizeof(mp));
       for(int i=0;i<m;i++)
       {
           int u,v,w;
           scanf("%d %d %d",&u,&v,&w);
           if(w<mp[u][v])
              mp[u][v]=mp[v][u]=w;//保留下最小值,因为是最短路径,可能题目说的不太清楚,人家不会有短的路不走,走最长的路
       }         
       
       int ans=0;
       for(int i=2;i<=n-1;i++)
       {
            int temp=dijkstra(i);//开始删除点
            if(temp==INF)//说明这个人成功的阻挡了,那就可以直接退出了
            {
                ans=INF;
                break;
            }
            else//阻挡不了
            {
                if(temp>ans)//我们找的这个人要让它花最多的钱,temp只不过是在删掉一个人的情况下的最小值,我们找的是删人之后的最大值。
                {
                    ans=temp;
                }
            }
      }
      if(ans==INF)
         printf("Inf\n");
      else
         printf("%d\n",ans);
    }
      return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值