ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Roadblocks POJ 3255(次短路)

2019-08-27 16:38:09  阅读:247  来源: 互联网

标签:dis2 int 短路 3255 Roadblocks POJ push include dis


原题

题目链接

题目分析

给无向图,求次短路.相对于第k短路而言次短路还是好求的,只需要在跑dijkstra的过程中顺便记录次短路就行了.

代码

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <utility>
 6 #include <ctime>
 7 #include <cmath>
 8 #include <cstring>
 9 #include <string>
10 #include <stack>
11 #include <queue>
12 #include <vector>
13 #include <set>
14 #include <map>
15 
16 using namespace std;
17 typedef long long LL;
18 const int INF_INT=0x3f3f3f3f;
19 const LL INF_LL=0x3f3f3f3f3f3f3f3f;
20 
21 typedef pair<int,int> P;
22 struct edge{int to,cost;};
23 vector<edge> es[6000];
24 int dis[6000],dis2[6000];
25 int n,r;
26 
27 void add()
28 {
29     int s,t,c;
30     scanf("%d %d %d",&s,&t,&c);
31     es[s].push_back(edge{t,c});
32     es[t].push_back(edge{s,c});
33 }
34 
35 void dijkstra()
36 {
37     priority_queue<P,vector<P>,greater<P> > que;
38     for(int i=1;i<=n;i++) dis[i]=dis2[i]=INF_INT;
39     dis[1]=0;
40     que.push(P(0,1));
41     while(que.size())
42     {
43         P p=que.top();que.pop();
44         int x=p.second;
45         if(p.first>dis2[x]) continue;
46         for(int i=0;i<es[x].size();i++)
47         {
48             edge t=es[x][i];
49             int d=p.first+t.cost;
50             if(dis[t.to]>d) swap(dis[t.to],dis2[t.to]),dis[t.to]=d,que.push(P(d,t.to));
51             else if(dis2[t.to]>d) dis2[t.to]=d,que.push(P(d,t.to));
52         }
53     }
54 }
55 
56 int main()
57 {
58 //    freoGen("black.in","r",stdin);
59 //    freopen("black.out","w",stdout);
60     cin>>n>>r;
61     for(int i=0;i<r;i++) add();
62     dijkstra();
63     printf("%d\n",dis2[n]);
64     return 0;
65 }

 

标签:dis2,int,短路,3255,Roadblocks,POJ,push,include,dis
来源: https://www.cnblogs.com/VBEL/p/11419177.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有