Roadblocks POJ 3255(次短路)

原题

题目链接

题目分析

给无向图,求次短路.相对于第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 }

原文地址:https://www.cnblogs.com/VBEL/p/11419177.html

时间: 2024-08-10 22:37:49

Roadblocks POJ 3255(次短路)的相关文章

Roadblocks(POJ 3255)

原题如下: Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19314   Accepted: 6777 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old hom

POJ 3255 Roadblocks (次短路问题)

解法有很多奇葩的地方,比如可以到达终点再跳回去再跳回来(比如有两个点)....反正就是不能有最短路,不过没关系,算法都能给出正确结果 思想:和求最短路上的点套路一样,spfa先正着求一次,再反着求一次最短路,然后枚举每条边<i,j>找dist_zheng[i] + len<i,j> + dist_fan[j]的第二小值即可!注意不能用邻接矩阵,那样会MLE,应该用邻接表 /* poj 3255 3808K 266MS */ #include<cstdio> #inclu

poj 3255 Roadblocks【次短路】

题目:poj 3255 Roadblocks 题意:给出一个无向图,然后求1到n点的次短路 分析:两种做法,第一种,Astat+最短路求k短路的方法. 第二种是比较暴力的方法. 先求1点到所有点的最短路dis1 然后求n点到所有点的最短路dis2 然后枚举所有边,则次短路为dis1[from] + dis2[to] + w[i]中大于最短路的最短的. AC代码: #include <cstdio> #include <string> #include <cstring>

POJ - 3255 Roadblocks (次短路)

POJ - 3255 Roadblocks Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit Status Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her

POJ 3255 &amp;&amp; HDU 1688 &amp;&amp; HDU 3191 次短路问题

POJ 3255 Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7627   Accepted: 2798 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old h

如何用求次长边——POJ 3255 题解

题目大意 给你一个 \(n\) 个点,\(m\) 条边的无向图,求出这个无向图中从1到n的次短路.其中\(n \le 5000\),\(m \le 100000\). 题目传送门 POJ 3255 思路 其实求次长路是很简单的一个问题,但是网上却有很多算法都过于复杂了.首先我们先求出从1到每个结点的最短路长度(用Dijkstra或者是SPFA都可以),存入数组 \(dis1\) 中,然后再求出从结点 \(n\) 到任意结点的最短路的长度,存入数组 \(dis2\) 中. 然后我们枚举这个图中的所

poj 1502 最短路+坑爹题意

链接:http://poj.org/problem?id=1502 MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5249   Accepted: 3237 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed share

POJ 2448(K短路,A*+SPFA) Remmarguts&#39; Date

题意 给一个n个点m条边的图,然后给一个起点和一个终点,求起点到终点的第K短路. 思路 求第K短路.一个经典的问题. SPFA+A* 核心思想在A*搜索的估计函数的建立上. F(x) = g(x) + h(x) 估价函数 = s到x的距离 + x到t的距离 估价函数的含义就是经过x这个点的路径的距离. 我们在搜索的时候每次选择估价函数较小的值,进行拓展.这样我们搜索到t点的状态出来顺序就是,最短路-次短路-.第三短路- 就减少了我们搜索的状态数. 代码实现上,实现一个估价函数的结构体,然后是实现

poj 1135 最短路 dijkstra

传送门 http://poj.org/problem?id=1135 建模分两部分:1.如果最后是关键牌倒下,那么找最短路中最长的就行--最远的倒下,其他的牌一定倒下,所以找最远的最短路 2.如果最后是普通牌倒下,那么找三角形,三角形周长的一半就是倒下的位置 到底是情况1还是情况2,自己在脑子模拟一下就能想到,还是那句话,最难倒下的倒下了,其他的一定都倒下了,若第二种情况的时间比第一种长,那么就是第二种,反之,第一种 上代码 /**********************************