UVALive Problem 7079 How Many Maos Does the Guanxi Worth(暴力枚举+最短路)——2014ACM/ICPC亚洲区广州站

来源:互联网 发布:尼康处理raw软件 编辑:程序博客网 时间:2024/05/17 02:45

此文章可以使用目录功能哟↑(点击上方[+])

 UVALive Problem 7079 How Many Maos Does the Guanxi Worth

Accept: 0    Submit: 0
Time Limit: 3.000 seconds

 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

 Problem Idea

解题思路:

【题意】
刘老板要通过他的关系网买通校长

规则如下:

①刘老板->朋友A->朋友B->……->校长,整个过程需要的花费均有刘老板来出

②关系网错综复杂,所以买通校长的路线不止一条

现在,为了破坏刘老板买通校长的计划,我们要除去刘老板关系网中的一个人,使得刘老板的花费尽可能大

(不能除去刘老板或校长)

问刘老板最大花费是多少


【类型】
暴力枚举+最短路

【分析】
对于刘老板本人来说,必定想要少花钱

所以在关系网中,它必定会沿着最短路来打通与校长的关系

而我们要完成的则是使刘老板的最短路尽可能长

因为关系网中结点最多只有30个(除去刘老板和校长,最多28个)

那我们完全可以暴力枚举删除某个结点之后,求最短路,取值最大的那个

如样例1




所以,结果为50

而最短路的求法则有很多,dijkstra or dijkstra+堆优化 or spfa 等都是可以的

本人采用的是dijkstra+堆优化(优先队列实现)

【时间复杂度&&优化】
O(n^2logn)

题目链接→UVALive Problem 7079 How Many Maos Does the Guanxi Worth

 Source Code

/*Sherlock and Watson and Adler*/#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<bitset>#include<cmath>#include<complex>#include<string>#include<algorithm>#include<iostream>#define eps 1e-9#define LL long long#define PI acos(-1.0)#define bitnum(a) __builtin_popcount(a)using namespace std;const int N = 35;const int M = 1005;const int inf = 1000000007;const int mod = 1000003;struct edge{    int v,c,to;}e[2*M];struct node{    int v,c;    node(){}    node(int _v,int _c):v(_v),c(_c){}    bool operator < (const node &a) const    {       return c>a.c;//最小值优先    }};int n,m,p,h[N],a[M],b[M],c[M],d[N];bool v[N];void add_edge(int u,int v,int c){    e[p].v=v;    e[p].c=c;    e[p].to=h[u];    h[u]=p++;}int dijkstra(int k){    int i;    node u;    p=0;    priority_queue<node> q;    memset(h,-1,sizeof(h));    memset(v,false,sizeof(v));    for(i=1;i<=n;i++)        d[i]=inf;    d[1]=0;    for(i=0;i<m;i++)        if(a[i]!=k&&b[i]!=k)        {            add_edge(a[i],b[i],c[i]);            add_edge(b[i],a[i],c[i]);        }    q.push(node(1,0));    while(!q.empty())    {        u=q.top();        q.pop();        if(v[u.v])            continue;        v[u.v]=true;        for(i=h[u.v];i+1;i=e[i].to)        {            d[e[i].v]=min(d[e[i].v],u.c+e[i].c);            q.push(node(e[i].v,d[e[i].v]));        }    }    return d[n];}int main(){    int i,ans;    while(scanf("%d%d",&n,&m)&&(n||m))    {        ans=0;        for(i=0;i<m;i++)            scanf("%d%d%d",&a[i],&b[i],&c[i]);        for(i=2;i<n;i++)            ans=max(ans,dijkstra(i));        if(ans!=inf)            printf("%d\n",ans);        else            puts("Inf");    }    return 0;}
菜鸟成长记

0 0
原创粉丝点击