POJ
来源:互联网 发布:淘宝新店流量扶持 编辑:程序博客网 时间:2024/06/05 14:07
题目链接:http://poj.org/problem?id=3255(《挑战》)
题意:就是求1到n的次短路
思路:同时求出最短路和次短路,如何判断次短路呢?要么就是1到某个非终点的u的最短路,再加上d(u ,v ),要么是1到某个非终点的u的次短路,再加上d(u ,v ),只有这两种可能
(具体可以看代码的注释)
代码:
#include <cstdio>#include <cmath>#include <iostream>#include <cstring>#include <algorithm>#include <queue>#include <stack>#include <vector>#include <map>#include <numeric>#include <set>#include <string>#include <cctype>#include <sstream>#define INF 0x3f3f3f3fusing namespace std;typedef long long LL;typedef pair<int, int> P;const int maxn = 5e3 + 5;int n,r;struct edge{ int to,cost;};vector<edge>G[maxn];int d[maxn];int dd[maxn];void dijk(){ priority_queue<P,vector<P>,greater<P> >q; memset(d,INF,sizeof(d)); memset(dd,INF,sizeof(dd)); d[1]=0; q.push(P(0,1)); while (!q.empty()){ P p=q.top(); q.pop(); int v=p.second,dis=p.first; if (dd[v]<dis) continue;//舍弃被次短路还大的值 for (int i=0;i<G[v].size();i++){ edge e=G[v][i]; int d2=dis+e.cost; if (d[e.to]>d2){ swap(d[e.to],d2); //更新最短路 q.push(P(d[e.to],e.to)); } if (dd[e.to]>d2&&d[e.to]<d2){ //长度在最短路和次短路之间则可以更新次短路 dd[e.to]=d2; q.push(P(dd[e.to],e.to)); } } }}int main () { //freopen ("in.txt", "r", stdin); while (~scanf ("%d%d",&n,&r)){ int u,v,c; while (r--){ scanf ("%d%d%d",&u,&v,&c); G[u].push_back(edge{v,c}); G[v].push_back(edge{u,c}); } dijk(); printf ("%d\n",dd[n]); } return 0;}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- Segmentation As Selective Search for Object Recognition
- ZoomIt的使用方法
- Vue 折腾记
- VC读写TXT文件和CStdioFile读写乱码----本人亲自测试可行
- 漫画加载,频道管理(Myadapter)
- POJ
- Windows“启动”文件夹
- linux学习笔记之linux系统启动过程
- Hadoop 安装指南
- spring-配置bean(2、基于JavaConfig的配置)
- hadoop 小问题环境设置
- 漫画加载,频道管理(MyFragment)
- AtCoder regular contest 081 E
- 害死人不偿命的-3n-1-猜想