hdu5137 How Many Maos Does the Guanxi Worth(单源最短路径)

题目链接:点击打开链接

题目描述:现在有一张关系网,网中有n个结点标号为1~n,有m个关系,每个关系之间有一个权值。问从2~n-1中任意去掉一个结点之后,从1~n的距离中所有最小值的最大值为多少?

解题思路:多次调用Dijkstra即可,每次标记那个结点不通即可

代码:

#include <cstdio>
#include <queue>
#include <cstring>
#define MAXN 31
#define MAXE 1010
#define INF 1e9+7
using namespace std;
int head[MAXN];
struct Edge{
    int to,cost,next;
}edge[MAXE*2];
struct node{
    int ct;
    int cost;
    node(int _ct,int _cost):ct(_ct),cost(_cost){}
    bool operator<(const node& b)const{///注意优先权队列:<代表大顶堆,>代表小顶堆
        return cost>b.cost;
    }
};
int tol;
void addEdge(int x,int y,int cost){
    edge[tol].to=y;
    edge[tol].cost=cost;
    edge[tol].next=head[x];
    head[x]=tol++;

    edge[tol].to=x;
    edge[tol].cost=cost;
    edge[tol].next=head[y];
    head[y]=tol++;
}
int n,m;
int dis[MAXN];
bool vis[MAXN];
void bfs(int nt){
    memset(vis,false,sizeof(vis));
    vis[nt]=true;
    for(int i=1;i<=n;++i)
        dis[i]=INF;
    dis[1]=0;
    priority_queue<node> pq;
    while(!pq.empty()) pq.pop();
    pq.push(node(1,0));
    while(!pq.empty()){
        node tmp=pq.top();
        pq.pop();
        int u=tmp.ct;
        if(vis[u]) continue;
        vis[u]=true;
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].to;
            int cost=edge[i].cost;
            if(!vis[v]&&dis[v]>dis[u]+cost){
                dis[v]=dis[u]+cost;
                pq.push(node(v,dis[v]));
            }
        }
    }
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF&&(n!=0||m!=0)){
        tol=0;
        memset(head,-1,sizeof(head));
        int x,y,cost;
        for(int i=1;i<=m;++i){
            scanf("%d%d%d",&x,&y,&cost);
            addEdge(x,y,cost);
        }
        int ans=0;
        for(int i=2;i<n;++i){
            bfs(i);
            ans=max(ans,dis[n]);
        }
        if(ans<INF)
            printf("%d\n",ans);
        else
            printf("Inf\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-10 19:16:20

hdu5137 How Many Maos Does the Guanxi Worth(单源最短路径)的相关文章

HDU5137 How Many Maos Does the Guanxi Worth(枚举+dijkstra)

How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)Total Submission(s): 760    Accepted Submission(s): 290 Problem Description "Guanxi" is a very important word in Chinese. It

ACM学习历程——HDU5137 How Many Maos Does the Guanxi Worth(14广州10题)(单源最短路)

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 ma

HDU5137 How Many Maos Does the Guanxi Worth (13广州现场赛K题 )--最短路问题

链接:click here 题意:从2~n-1这几个点中任意去掉一个点,使得从1到n的最短路径最大,如果任意去掉一个点1~n无通路输出Inf. 去年这道题现场是队友1A过的,感觉好NB,当时还不会最短路问题,只是脑子里大概有这么个印象,做了之发现数据比较水,今天看了一下Dijkstra 和Floyd,用两种算法实现了一下: 题目描述有点长,但是搞清楚了,就很容易了,因为数据实在很水,算是最短路入门训练的一道题了 Dijkstra 算法 (1)令S={源点s + 已经确定了最短路径的顶点vi} (

How Many Maos Does the Guanxi Worth

How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)Total Submission(s): 864    Accepted Submission(s): 329 Problem Description "Guanxi" is a very important word in Chinese. It

hdu 5137 How Many Maos Does the Guanxi Worth(dijkstra &amp;&amp; floyd)

How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 853    Accepted Submission(s): 326 Problem Description "Guanxi" is a very important word in Chinese. I

HDU 5137 How Many Maos Does the Guanxi Worth(暴力+spfa)

题目大意:给你一个关系图,让你再删除一个点之后(除了1,和n).让你求出来从1到n的最短路的最大值,如果不可达输出Inf. 解题思路:题意也说了,就是暴力枚举点,然后跑n-2次spfa求出来一个最大值就可以了. How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 4

hdoj 5137 How Many Maos Does the Guanxi Worth 【枚举删点 + 最短路】

How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 847    Accepted Submission(s): 322 Problem Description "Guanxi" is a very important word in Chinese. I

HDU 5137 How Many Maos Does the Guanxi Worth (14广州 Floyd 最短路)

How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 468    Accepted Submission(s): 164 Problem Description "Guanxi" is a very important word in Chinese. I

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

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