poj1125最短路模板
来源:互联网 发布:软件著作权办理时间 编辑:程序博客网 时间:2024/05/22 17:48
简单的模板应用,就是求图中各个点中,每个点到其余各点最大距离的最小值。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 110#define maxint 999999int mapp[maxn][maxn];//the map of the problemint ans[maxn];//the longest distance of every pointint dist[maxn];//the distance between the v and the other pointint n;int Dijsktra(int v){ bool s[maxn]; int i,j; for(i=1;i<=n;i++) { dist[i]=mapp[v][i]; s[i]=false; } dist[v]=0; s[v]=true; for(i=2;i<=n;i++) { int tmp=maxint; int u=v; for(j=1;j<=n;j++) if(!s[j]&&dist[j]<tmp) { u=j;tmp=dist[j]; } s[u]=1; for(j=1;j<=n;j++) if(!s[j]&&mapp[u][j]<maxn) { int newdist=dist[u]+mapp[u][j]; if(newdist<dist[j]) { dist[j]=newdist; } } } int max=0; for(i=1;i<=n;i++){ if(dist[i]>max) max=dist[i]; } return max;}int main(){ int m,i,j,a,b,min,f; while(scanf("%d",&n)!=EOF) { if(n==0) break; memset(mapp,maxint,sizeof(mapp)); for(j=1;j<=n;j++) { scanf("%d",&m); for(i=1;i<=m;i++) { scanf("%d%d",&a,&b); mapp[j][a]=b; } } for(i=1;i<=n;i++) {ans[i]=Dijsktra(i);} min=maxint; for(i=1;i<=n;i++) if(ans[i]<min) { min=ans[i]; f=i; } printf("%d %d\n",f,min); } return 0;}
看了别人的代码,才发现这题的数据好弱啊,我都没有考虑不通的情况都过了。
#include <iostream>#include <cstring>using namespace std;const int MAX_VERTEX=128;const int MAX_EDGE_WEIGHT=100000000;int nv;//the number of vertexint gam[MAX_VERTEX][MAX_VERTEX];//the map of the problemint spd[MAX_VERTEX];//the distance between source point and the destination pointint asd[MAX_VERTEX];//whether the point is exploredint ansperson,anstime;int main(){ int n,p,t,i,j,k; while(cin>>nv&&nv!=0) { for(i=1;i<=nv;i++) for(j=1;j<=nv;j++) gam[i][j]=MAX_EDGE_WEIGHT; for(i=1;i<=nv;i++) { cin>>n; for(j=1;j<=n;j++) { cin>>p>>t;gam[i][p]=t; } } ansperson=-1;anstime=MAX_EDGE_WEIGHT; for(k=1;k<=nv;k++) { for(i=1;i<=nv;i++) { if(i==k) asd[i]=1; else asd[i]=0; spd[i]=gam[k][i]; } for(i=1;i<=nv-2;i++) { int w=MAX_EDGE_WEIGHT,m=k; for(j=1;j<=nv;j++) { if(asd[j]==0&&w>spd[j]) { w=spd[j];m=j; } } if(m!=k) asd[m]=1; else break; for(j=1;j<=nv;j++) { if(asd[j]==0&&spd[j]>spd[m]+gam[m][j]) spd[j]=spd[m]+gam[m][j]; } } int time=0; for(i=1;i<=nv;i++) if(i!=k&&time<spd[i]) time=spd[i]; if(anstime>time) { ansperson=k;anstime=time; } } if(anstime!=MAX_EDGE_WEIGHT) cout<<ansperson<<' '<<anstime<<endl; else cout<<"disjoint"<<endl; } return 0;}
- poj1125最短路模板
- poj1125 最短路floyd
- poj1125(floyd最短路)
- 【最短路】poj1125 Floyd
- poj1125(Floyd)最短路问题
- poj1125 floyed 求最短路
- poj1125 Stockbroker Grapevine 最短路
- 【POJ1125】Stockbroker Grapevine 最短路
- 最短路(poj1502 poj3259 poj1125)
- poj1125(最短路floyd算法)
- poj1125 Stockbroker Grapevine-最短路
- 图论专题(二)POJ1125最短路
- 最短路--floyed-warshall算法poj1125
- POJ1125 Stockbroker Grapevine(最短路Floyed)
- POJ1125 Stockbroker Grapevine(最短路floyd算法)
- poj1125 Stockbroker Grapevine 最短路 Floyd 思考
- POJ1125 股市谣言传播 图论(Floyd最短路)
- poj1125 Stockbroker Grapevine 最短路 dijkstral + 优先队列
- WinpCap过滤串表达式的语法
- oracle字段拼接
- perl使用xml::simple来读写xml
- Linux 下TCP连接关闭情况分析
- oracle转义
- poj1125最短路模板
- Oracle中TO_DATE TO_CHAR格式
- 第十周实验报告2
- 一种优化的字符串排序 泛型算法
- 为指针分配和释放空间
- 第十周任务一(派生类的定义与应用)
- poj 3321 apple tree
- 什么是ORACLE数据库的事务
- 第十周作业3