How Many Maos Does the Guanxi Worth (暴力枚举+Dijkstra)

来源:互联网 发布:c语言abs函数 编辑:程序博客网 时间:2024/05/28 15:52
"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

题意:

刘老板要通过自己的关系网给学校校长送礼,找人办事是要花钱的,但是你要劝说关系中一个人不要参与,

使刘老板花最多的钱(刘老板自己想要花 的钱最少)

1到n个人,在删除一个人(除第1个人和第n个人外)情况下,求打通关系从第一个1到第n人所花费最多的钱。

如果刘老板不能达到自己的目的输出 Inf

代码:

#include<stdio.h>#include<string.h>#define Inf 0x3f3f3f3f#define MAX 5000using namespace std;int Dijkstra(int k);int e[MAX][MAX];int n,m;int main(){    while(scanf("%d%d",&n,&m)!=EOF){        if(n==0&&m==0) break;        memset(e,Inf,sizeof(e));        int i;        for(i=0;i<=n;i++)            e[i][i]=0;        while(m--){            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            if(c<e[a][b])            e[a][b]=e[b][a]=c;        }        int cnt=0;        for(i=2;i<n;i++)   //枚举可以去掉的点  但是不可以去掉1点和n点            cnt=cnt>Dijkstra(i)?cnt:Dijkstra(i); //取最小值中的最大值        if(cnt==Inf) printf("Inf\n");        else printf("%d\n",cnt);    }return 0;}int Dijkstra(int k){  //由于刘老板要使到n的钱花最少,所以求单源最短路    int book[MAX];    int dis[MAX];    memset(book,0,sizeof(book));    int i;    for(i=1;i<=n;i++)        dis[i]=e[1][i];    book[1]=1;    book[k]=1;  //学会的新技能 删掉那个点 就把那个点book记为1  不再用他来松弛其他点    int j;          for(i=1;i<=n;i++){   //Dijkstra算法核心        int min=Inf;        int u;        for(j=1;j<=n;j++){            if(dis[j]<=min&&!book[j]){                min=dis[j];                u=j;            }        }        book[u]=1;        for(j=1;j<=n;j++){   //松弛操作            if(e[u][j]<Inf){                if(dis[j]>dis[u]+e[u][j])                    dis[j]=dis[u]+e[u][j];            }        }    }    return dis[n];}
邻接表存图
#include<stdio.h>#include<string.h>#define Inf 0x3f3f3f3f#define MAX 1005using namespace std;int Dijkstra(int x);struct node{    int u,v,w;  //边关系u起点 v终点 w 权重      int next;   //记录当前存入边的上一条}e[MAX];int head[40];  //head[2]=5 当前以2为起点的边在 e 中是第5条int dis[40];   int cnt=1;     //记录e中的边数int n,m;void add(int a,int b,int c){  //加入边    e[cnt].u=a;    e[cnt].v=b;    e[cnt].w=c;    e[cnt].next=head[a];    head[a]=cnt++;}int main(){    while(scanf("%d%d",&n,&m)!=EOF){        if(m==0&&n==0) break;        memset(e,Inf,sizeof(e));        memset(head,-1,sizeof(head));        int i;        for(i=1;i<=m;i++){            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            add(a,b,c); //无向图            add(b,a,c);        }        int max=0;        for(i=2;i<n;i++){  //遍历可以删除的点            int k=Dijkstra(i);            max=max>k?max:k;        }        if(max==Inf) printf("Inf\n");        else  printf("%d\n",max);    }return 0;}int Dijkstra(int x){    memset(dis,Inf,sizeof(dis));    int i;    for(i=head[1];i!=-1;i=e[i].next){        dis[e[i].v]=e[i].w;    }    dis[1]=0;    int book[MAX];    memset(book,0,sizeof(book));    book[1]=book[x]=1; //把要删除的点标记为1    int j;    for(i=1;i<=n;i++){        int min=Inf;        int u;        for(j=1;j<=n;j++){            if(!book[j]&&min>=dis[j]){                min=dis[j];                u=j;            }        }        book[u]=1;        for(j=head[u];j!=-1;j=e[j].next){            if(e[j].w<Inf){                if(dis[e[j].v]>dis[u]+e[j].w)                    dis[e[j].v]=dis[u]+e[j].w;            }        }    }    return dis[n]; //返回从1到n的距离 }




阅读全文
0 0