HDU 3631 Shortest Path(Floyd变形)
来源:互联网 发布:佛教用品淘宝店 编辑:程序博客网 时间:2024/04/30 00:09
HDU 3631 Shortest Path(Floyd变形)
http://acm.hdu.edu.cn/showproblem.php?pid=3631
题意:给你一个N个节点的有向图以及M条边.然后我们要回答Q条询问,询问是问指定两点A与B的最短距离.但是A与B的最短距离只能经过已经被标记了的点(就算A与B也必须被标记了).
分析:
我们先读入所有的初始距离矩阵.然后每标记一个点k,我们就用k去松弛所有以k作为中间节点的边.(其实这就是一个Floyd的最外层循环).
这样我们当前得到的最短距离d[i][j]=x,我们能保证当前从i到j的最短距离路径中,这条路径的所有中间节点肯定是被标记了的.但是这条路径的端点(即i和j)不一定被标记了.如果i与j被标记了,那么直接输出.如果i与j还没有被标记,那么出错.(仔细想想)
AC代码:
#include<cstdio>#include<algorithm>#include<cstring>#define INF 1e9using namespace std;const int maxn=300+10;int n,m,t;int d[maxn][maxn];bool vis[maxn];void floyd(int k){ for(int i=0;i<n;i++) for(int j=0;j<n;j++)if(d[i][k]<INF && d[k][j]<INF) d[i][j] =min(d[i][j],d[i][k]+d[k][j]);}int main(){ int kase=0; while(scanf("%d%d%d",&n,&m,&t)==3) { if(n==0&&m==0&&t==0) break; if(kase!=0) printf("\n"); printf("Case %d:\n",++kase); for(int i=0;i<n;i++) for(int j=0;j<n;j++) d[i][j]= i==j?0:INF; memset(vis,0,sizeof(vis)); while(m--) { int u,v,dist; scanf("%d%d%d",&u,&v,&dist); d[u][v] = min(d[u][v],dist); } while(t--) { int type,x,y; scanf("%d",&type); if(type==0) { scanf("%d",&x); if(vis[x]) printf("ERROR! At point %d\n",x); else { vis[x]=true, floyd(x); }//只用新标记的点去松弛 } else { scanf("%d%d",&x,&y); if(!vis[x]||!vis[y]) printf("ERROR! At path %d to %d\n",x,y); else if(d[x][y]==INF) printf("No such path\n"); else printf("%d\n",d[x][y]); } } } return 0;}
0 0
- HDU 3631 Shortest Path(Floyd变形)
- 【HDU】3631 Shortest Path 【floyd】
- hdu 3631 Shortest Path (floyd)
- hdu 3631 Shortest Path【Floyd】
- hdu 3631 Shortest Path (floyd)
- HDU 3631 Shortest Path Floyd
- HDU 3631Shortest Path(flody变形)
- hdu 3631 Shortest Path(floyd插点法)
- hdu 3631 Shortest Path(Floyd)
- hdu 3631 Shortest Path(Floyd)
- HDU - 3631 Shortest Path(Floyd最短路)
- hdu 3631 Shortest Path floyd 解题报告
- hdu 3631 Shortest Path (floyd)
- hdu3631 Shortest Path (floyd 变形)
- HDU 5636:Shortest Path floyd
- HDU 5636:Shortest Path【Floyd】
- HDU 3631(Shortest Path) 最短路问题 (Floyd)
- HDU 3631 Shortest Path (Floyd的深层理解)
- UVA 10474 Where is the Marble?
- 解决安装软件时的/usr/local/lib/libz.a(compress.o)错误
- 可信云 政府采购前奏
- 回到顶部
- 黑马程序员:TreeSet的特征
- HDU 3631 Shortest Path(Floyd变形)
- Ruby基础知识-赋值和条件运算符
- Android中关于Volley的使用(二)加载Json数据
- Python学习-包
- OpenGLES demo - 10. DrawElements和VBO
- 学习window编程1
- 阿的范德萨更规范和房间
- [LeetCode] String to Integer (atoi)
- AndroidManifest.xml简析