您的位置:首页 > 其它

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

2015-08-15 09:51 344 查看
题目链接:点击打开链接

题目描述:现在有一张关系网,网中有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
);
}
if(ans<INF)
printf("%d\n",ans);
else
printf("Inf\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: