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;
}
posted on 2017-06-11 14:03 cynchanpin 阅读(142) 评论(0) 收藏 举报
浙公网安备 33010602011771号