hdu5137 How Many Maos Does the Guanxi Worth(单源最短路径)
题目链接: pid=5137">点击打开链接
题目描写叙述:如今有一张关系网。网中有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;
}
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/5 ...
- 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 &quo ...
- hdu 5137 How Many Maos Does the Guanxi Worth 最短路 spfa
How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/5 ...
- HDU 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/5120 ...
- 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/5 ...
- How Many Maos Does the Guanxi Worth
How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/5 ...
- HDU 5137 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/5 ...
- (hdoj 5137 floyd)How Many Maos Does the Guanxi Worth
How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/5 ...
- 杭电5137How Many Maos Does the Guanxi Worth
How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/5 ...
随机推荐
- Spark机器学习之MLlib整理分析
友情提示: 本文档根据林大贵的<Python+Spark 2.0 + Hadoop机器学习与大数据实战>整理得到,代码均为书中提供的源码(python 2.X版本). 本文的可以利用pan ...
- 前端实现input[type='file']上传图片预览效果
众所周知JavaScript在设计上处于安全角度考虑,是不允许读写本地文件的(原因请自行百度): 但是在实际项目应用中,经常会使用到上传图片,并且可以让用户直接预览图片.对于此种做法有两种方法可以实现 ...
- 【hdu 6321】Dynamic Graph Matching
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] DP 设f[i][j]表示前i个操作,已经匹配了的点的状态集合为j的方案数 对于+操作 有两种情况. 1.这条边作为匹配的边 2.这 ...
- 数据结构实现(四)二叉查找树java实现
转载 http://www.cnblogs.com/CherishFX/p/4625382.html 二叉查找树的定义: 二叉查找树或者是一颗空树,或者是一颗具有以下特性的非空二叉树: 1. 若左子树 ...
- ActiveMQ_Windows和Linux版本的安装部署
1, 保证电脑上安装了jdk6以上版本的java,并配置了好环境变量 : 2, 官方下载地址:http://activemq.apache.org/download-archives.html ,这里 ...
- Android实战简易教程-第十三枪(五大布局研究)
我们知道Android系统应用程序通常是由多个Activity组成,而这些Activity以视图的形式展如今我们面前, 视图都是由一个一个的组件构成的. 组件就是我们常见的Button.TextEdi ...
- php设计模式之桥接模式
php设计模式之桥接模式 一.概述 桥接模式:将两个原本不相关的类结合在一起,然后利用两个类中的方法和属性,输出一份新的结果. 其实就是讲不相关的东西通过类(本例中是SendInfo)结合在一起,从而 ...
- JSP中动态include与静态include的区别介绍
转自:https://m.jb51.net/article/43304.htm 动态INCLUDE 用法:<jsp:include page="included.jsp" f ...
- POJ 3048 线性筛法求素数
一个坑: 有组数据如下: 1 1 坑很深-- //By SiriusRen #include <cstdio> #define N 200000 using namespace std; ...
- Spring深入浅出(三)XML方式以及注解的方式操作IOC
在日常的开发过程中,我们把程序分为3层:Controller层,Service层,DAO层.Controller类似于Servlet,也就是MVC中的控制层. 调用的顺序是: Controller层调 ...