您的位置:首页 > 其它

hdu1537How Many Maos Does the Guanxi Worth【最短路&&最短路径最大化】~

2015-08-24 11:23 423 查看

How Many Maos Does the Guanxi Worth

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)

Total Submission(s): 1049 Accepted Submission(s): 365



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 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 【题目大意】:一个老板想通过层层关系让他的孩子上学,他需要通过关系找一些人,这些人再通过关系找另一些人帮忙,最后要找到学校的校长,找人办事,老板要给他们钱,但是你特别讨厌这个老板,想尽全力破坏他的计划,所以你要从些人里边劝走一个人,打破他的计划,最好是少了这个人老板无论给多少钱都办不成这件事,如果少了谁都能办成这件事的话,那就去掉一个人,使去掉这个人后,老板花的钱最多。输入:有多组测试数据,在每组测试数据中:第一行输入两个整数N和M,N代表有N个人在老板的关系网中,老板是1,校长是N;M代表有M层关系在老板的关系网中;然后有M行,每行有三个整数A,B,C,表示A和B有一定的关系,如果A找B或B找A帮忙,老板将付给帮忙者C钱。如果N,M同时为0,输入结束。(输入确保如果你没有捣乱,老板的计划一定能成功)输出:对于每组数据,输出你捣乱后老板需要付的钱数,如果事情办不成,则输出Inf。【思路】:求从第2~N-1个人中去掉一个人后最短路径的最大值,还是最短路的变形,关键是去掉一个人如何实现。【考察点】:最短路径的最大化。【代码】:[code]#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <string.h>
using namespace std;

const int INF=0x3f3f3f3f;
const int maxm=1000;
const int maxn=110;
int cnt,head[maxm];
struct Edge
{
    int u,v,w,next;
}edge[maxm];
void add(int u,int v,int w)
{
	edge[cnt].u=u;
	edge[cnt].v=v;
	edge[cnt].w=w;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}
int n,m;
int dist[50];
int vis[50];
void SPFA(int s)
{
	queue<int>q;
	memset(dist,INF,sizeof(dist));
	memset(vis,0,sizeof(vis));
	q.push(s);
	dist[s]=0;
	vis[s]=1;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=head[u];i!=-1;i=edge[i].next)
		{
			int v=edge[i].v;
			if((dist[v]>dist[u]+edge[i].w))
			{
				dist[v]=dist[u]+edge[i].w;
				if(!vis[v])
				{
					vis[v]=1;
					q.push(v);
				}
			}
		}
	}
}
/**************以上为SPFA模板没有变化*****************/
int a[35][35];//定义一个二维数组存放两个人之间的关系值 
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(a,0,sizeof(a));
        if(n==0&&m==0)
            break;
        for(int i=0;i<m;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            a[x][y]=a[y][x]=z;//存放x,y两人的关系值 
        }
        int maxx=-1;
        for(int k=2;k<n;k++)//k为去掉的那个人的编号 
        {
             cnt=0;
             memset(head,-1,sizeof(head));
             for(int i=1;i<=n;i++)
             {
                 for(int j=1;j<=n;j++)
                 {
                     if(a[i][j]==0)//a[i][j]=0,说明两人没有关系,直接进入下一个循环 
                        continue;
                     if(i==k||j==k)//如果i或者j等于k,即为去掉的那个人,也相当于没有关系 
                        continue;//进入下一个循环 
                     add(i,j,a[i][j]);//建立关系 
                 }
             }
             SPFA(1);//执行SPFA 
             maxx=max(maxx,dist
);//取去掉某个人之后花费最大的路径 
        }
        if(maxx==INF)
            printf("Inf\n");
        else
            printf("%d\n",maxx);
    }
    return 0;
}

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: