码迷,mamicode.com
首页 > 其他好文 > 详细

[ACM] HDU 5137 How Many Maos Does the Guanxi Worth(去掉一个点使得最短路最大化)

时间:2014-12-22 11:16:28      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:acm   最短路   

How Many Maos Does the Guanxi Worth



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
 

Source


解题思路:

题意为有n个点(编号1-n),和m个关系组成的网络,默认情况下,总有一条路可以从1到达n,我们要做的就是去掉一个除了1和n之外的其他任何一个点,看能不能使得从1不能到达n,如果不能到达,就输出Inf就可以了,如果去掉任何一个点后都可以使得从1到达n,那么就输出这些最短路径中的最大的最短路径。

n最大为30,枚举每个点(1和n除外),删除该点求最短路径,可以求得 n-2个最短路径的值,如果发现有一种情况不能到达n,那么就输出Inf,否则输出这n-2个最短路径的值的最大值。

一开始按的是单向边做的,WA了两三次,后来又看了一遍题意,结果应该是双向边才可以。。。。一定要先弄清楚题意!

代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <time.h>
#include <iomanip>
#include <cctype>
using namespace std;
#define ll long long
const int maxn=32;
const int inf=0x3f3f3f3f;

int n,m;//节点数,有向边个数
int mp[maxn][maxn];
int dis[maxn];
bool vis[maxn];
bool ok;

void dijkstra(int start,int del)
{
    memset(dis,inf,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[start]=0;
    for(int i=1;i<=n;i++)
    {
        if(i==del)
            continue;
        int k,Min=inf;
        for(int j=1;j<=n;j++)
        {
            if(dis[j]<Min&&!vis[j])
            {
                Min=dis[j];
                k=j;
            }
        }
        vis[k]=1;
        for(int j=1;j<=n;j++)
        {
            if(j==del)
                continue;
            if(dis[k]+mp[k][j]<dis[j])
            dis[j]=dis[k]+mp[k][j];
        }
    }
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
    {
        int from,to,w;
        memset(mp,inf,sizeof(mp));
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&from,&to,&w);
            if(w<mp[from][to])
            {
                mp[from][to]=w;
                mp[to][from]=w;
            }
        }
        ok=0;//判断去掉一个点能不能使得图不连通
        int ans=-1;
        for(int i=2;i<=n-1;i++)
        {
            dijkstra(1,i);//枚举去掉的那一个点,求最短路
            if(dis[n]==inf)
                ok=1;
            if(ans<dis[n])//求最短路最长的那一条
                ans=dis[n];
        }
        if(ok==1)
        {
            printf("Inf\n");
            continue;
        }
        printf("%d\n",ans);
    }
    return 0;
}


先用DFS判断一下能不能从1到n

#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <time.h>
#include <iomanip>
#include <cctype>
using namespace std;
#define ll long long
const int maxn=32;
const int inf=0x3f3f3f3f;

int n,m;//节点数,有向边个数
int mp[maxn][maxn];
int dis[maxn];
bool vis[maxn];
bool ok;

void dijkstra(int start,int del)
{
    memset(dis,inf,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[start]=0;
    for(int i=1;i<=n;i++)
    {
        if(i==del)
            continue;
        int k,Min=inf;
        for(int j=1;j<=n;j++)
        {
            if(dis[j]<Min&&!vis[j])
            {
                Min=dis[j];
                k=j;
            }
        }
        vis[k]=1;
        for(int j=1;j<=n;j++)
        {
            if(j==del)
                continue;
            if(dis[k]+mp[k][j]<dis[j])
            dis[j]=dis[k]+mp[k][j];
        }
    }
}

void dfs(int s,int k)
{
    vis[s]=1;
    for(int i=1;i<=n;i++)
    {
        if(!vis[i]&&i!=k&&mp[s][i]!=inf)
            dfs(i,k);
    }

}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
    {
        int from,to,w;
        memset(mp,inf,sizeof(mp));
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&from,&to,&w);
            if(w<mp[from][to])
            {
                mp[from][to]=w;
                mp[to][from]=w;
            }
        }
        ok=0;//判断去掉一个点能不能使得图不连通
        int ans=-1;
        for(int i=2;i<=n-1;i++)
        {
            memset(vis,0,sizeof(vis));
            dfs(1,i);
            if(vis[n]==0)
            {
                ok=1;
                break;
            }
            dijkstra(1,i);//枚举去掉的那一个点,求最短路
            if(ans<dis[n])//求最短路最长的那一条
                ans=dis[n];
        }
        if(ok==1)
        {
            printf("Inf\n");
            continue;
        }
        printf("%d\n",ans);
    }
    return 0;
}


[ACM] HDU 5137 How Many Maos Does the Guanxi Worth(去掉一个点使得最短路最大化)

标签:acm   最短路   

原文地址:http://blog.csdn.net/sr_19930829/article/details/42076549

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!