poj3255
来源:互联网 发布:iis7怎么执行php 编辑:程序博客网 时间:2024/04/20 13:38
链接:点击打开链接
题意:求1到n的次短路是多少(次短路指的是比最短路长度长的次短的路径)
代码:
#include <iostream>#include <string.h>#include <algorithm>#include <stdio.h>#include <stdlib.h>#include <vector>#include <queue>#include <algorithm>#define INF 0x3f3f3f3fusing namespace std;struct node {int to,cost;};typedef pair<int,int>P;vector<node>G[5005];int dis1[5005],dis2[5005];int n,m;void dijkstra(int s){ int i,v,d,d2; node temp; priority_queue<P,vector<P>,greater<P> >que; fill(dis1,dis1+n,INF); fill(dis2,dis2+n,INF); dis1[s]=0; que.push(P(0,s)); while(!que.empty()){ P p=que.top();que.pop(); v=p.second;d=p.first; if(dis2[v]<d)continue; for(i=0;i<G[v].size();i++){ temp=G[v][i]; d2=temp.cost+d; if(dis1[temp.to]>d2){ swap(dis1[temp.to],d2); que.push(P(dis1[temp.to],temp.to)); } if(dis1[temp.to]<d2&&dis2[temp.to]>d2){ dis2[temp.to]=d2; //通过最短路也就是dis1数组将次短路保存到dis2数组,并入队 que.push(P(dis2[temp.to],temp.to)); } } }} //dijkstra模板using namespace std;int main(){ int i,x,y,z; node temp; while(scanf("%d%d",&n,&m)!=EOF){ for(i=0;i<n;i++) G[i].clear(); for(i=0;i<m;i++){ scanf("%d%d%d",&x,&y,&z); temp.to=y-1,temp.cost=z; //双向边 G[x-1].push_back(temp); temp.to=x-1; G[y-1].push_back(temp); } dijkstra(0); printf("%d\n",dis2[n-1]); } return 0;}
0 0
- POJ3255
- poj3255
- poj3255
- poj3255
- poj3255
- POJ3255
- poj3255 - Roadblocks
- POJ3255-Roadblocks
- poj3255 Roadblocks
- POJ3255 Roadblocks
- POJ3255-Roadblocks
- ***POJ3255-Roadblocks
- poj3255 Roadblocks
- poj3255——Roadblocks
- 次短路--poj3255
- POJ3255 Roadblocks , 次短路
- POJ3255 Roadblocks(次短路)
- poj3255 次短路
- 第一个java程序:错误: 编码GBK的不可映射字符
- Windows下如何构建和发布Python模块
- eclipse 无法链接手机
- android BrocadcastReceiver
- 第四十二天 BitmapView(蒙版,或模拟给图片打马赛克)
- poj3255
- 网络配置
- access vba打开另一个数据库并关闭当前数据库的代码
- 因果图小例子
- 在XML数据库中高效的关键字搜索求SLCAs
- Bash shell 快捷键
- iOS数据请求之post异步同步
- 网络图可视化工具
- c、c++重难点