题意:给定一个图,求一条1-n的次短路。

析:次短路就是最短路再长一点呗,我们可以和求最短路一样,再多维护一个数组,来记录次短路。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5000 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} vector<int> G[maxn], w[maxn];
int d1[maxn], d2[maxn]; int dijkstra(){
priority_queue<P, vector<P>, greater<P> > pq;
pq.push(P(0, 1));
memset(d1, INF, sizeof d1);
memset(d2, INF, sizeof d2);
d1[1] = 0; while(!pq.empty()){
P p = pq.top(); pq.pop();
int v = p.second, d = p.first;
if(d2[v] < d) continue;
for(int i = 0; i < G[v].size(); ++i){
int u = G[v][i];
int dd = d + w[v][i];
if(d1[u] > dd){
swap(dd, d1[u]);
pq.push(P(d1[u], u));
}
if(d1[u] == dd) continue;
if(d2[u] > dd){
d2[u] = dd; pq.push(P(d2[u], u));
}
}
}
return d2[n];
} int main(){
while(scanf("%d %d", &n, &m) == 2){
for(int i = 1; i <= n; ++i) G[i].clear(), w[i].clear();
for(int i = 0; i < m; ++i){
int u, v, val;
scanf("%d %d %d", &u, &v, &val);
G[u].push_back(v);
G[v].push_back(u);
w[u].push_back(val);
w[v].push_back(val);
}
printf("%d\n", dijkstra());
}
return 0;
}

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

  1. POJ 3255 Roadblocks (次级短路问题)

    解决方案有许多美丽的地方.让我们跳回到到达终点跳回(例如有两点)....无论如何,这不是最短路,但它并不重要.算法能给出正确的结果 思考:而最短的路到同一点例程.spfa先正达恳求一次,求的最短路径的 ...

  2. poj 3255 Roadblocks 次短路(两次dijksta)

    Roadblocks Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

  3. POJ 3255 Roadblocks (次短路 SPFA )

    题目链接 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...

  4. POJ 3255 Roadblocks(A*求次短路)

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12167   Accepted: 4300 Descr ...

  5. POJ 3255 Roadblocks (次短路模板)

    Roadblocks http://poj.org/problem?id=3255 Time Limit: 2000MS   Memory Limit: 65536K       Descriptio ...

  6. poj - 3225 Roadblocks(次短路)

    http://poj.org/problem?id=3255 bessie 有时会去拜访她的朋友,但是她不想走最快回家的那条路,而是想走一条比最短的路长的次短路. 城镇由R条双向路组成,有N个路口.标 ...

  7. 次最短路径 POJ 3255 Roadblocks

    http://poj.org/problem?id=3255 这道题还是有点难度 要对最短路径的算法非常的了解 明晰 那么做适当的修改 就可以 关键之处 次短的路径: 设u 到 v的边权重为cost ...

  8. poj 3255 Roadblocks

    Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13216 Accepted: 4660 Descripti ...

  9. POJ 3255 Roadblocks --次短路径

    由于次短路一定存在,则可知次短路一定是最短路中某一条边不走,然后回到最短路,而且只是一条边,两条边以上不走的话,就一定不会是次短路了(即以边换边才能使最小).所以可以枚举每一条边,算出从起点到这条边起 ...

随机推荐

  1. Ajax的跨域问题

    •跨域问题概述 •出于安全考虑,浏览器不允许ajax跨域获取数据 •可以通过script的src加载js的方式传递数据 fn({"a":"1","b& ...

  2. 九度OJ 1052:找x (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7335 解决:3801 题目描述: 输入一个数n,然后输入n个数值各不相同,再输入一个值x,输出这个值在这个数组中的下标(从0开始,若不在数 ...

  3. pip3 Fatal error in launcher: Unable to create process using '"' [转]

    在新环境上安装python的时候又再次遇到了这个情况,这次留意了一下,发现原来的文章有错误的地方,所以来更新一下,应该能解决大部分的问题. 环境是win8,原来只安装了python2.7.后来因为要用 ...

  4. 【题解】[CJOI2019Chebnear]

    [题解][CJOI2019Chebnear] 题目描述 给定平面上有\(n\)个仇人,\((x,y) ,x,y \in R^+\) ,\(n\)个人都是仇人关系,而有仇人关系的一对人的切比雪夫距离若\ ...

  5. 设计模式 - 单件模式(singleton pattern) 具体解释

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/28595349 单件模式(singleton ...

  6. SAP增强 和VA01相关增强点介绍

    -转 sap寻找用户出口方法 sap的用户出口总共有三代: 一.User EXIT 第一代的用户出口,它们include在SAP标准程序的源代码里,可以说他们是源代码的一部分,你改了这种出口就相当于改 ...

  7. rails debug

    =debug @thesis config下配置 东西需要重启之后才管用

  8. iOS 关于NSNotificationCenter

    通常我们在 iOS 中发生什么事件时该做什么是由 Delegate 实现的,  Apple 还为我们提供了另一种通知响应方式,那就是 NSNotification. NSNotificationCen ...

  9. EASYARM-IMX283 制作ubifs文件系统

    ubifs主页:http://www.linux-mtd.infradead.org/doc/ubifs.html nandflash上常用的文件系统有jffs2.yaffs和ubifs,其中ubif ...

  10. BZOJ 1726 [Usaco2006 Nov]Roadblocks第二短路:双向spfa【次短路】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1726 题意: 给你一个无向图,求次短路. 题解: 两种方法. 方法一: 一遍spfa,在s ...