http://poj.org/problem?id=3255

这道题还是有点难度

要对最短路径的算法非常的了解 明晰 那么做适当的修改 就可以

关键之处 次短的路径: 设u 到 v的边权重为cost

那么到v的次短路径要么是 到u的次短路径+cost;要么是到u的最短路径+cost;

那么就在dijkstra中 既要保存 最短路径的数组 又要 保存次短路径的情况

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#define MAXV 5007
#define MAXE 200007
#define INF 0x3f3f3f3f
//向前星 125ms
using namespace std;
typedef pair<int,int> P; int E, V;
struct Edge
{
int to, cost, next;
Edge () {}
Edge (int to, int cost, int next) : to(to), cost(cost), next(next) {}
}edge[MAXE];
int head[MAXV];
int num = ;
void Add(int from, int to, int cost)
{
edge[num] = Edge(to, cost, head[from]);
head[from] = num++;
} int dijkstra()
{
int dist1[MAXV], dist2[MAXE];
priority_queue<P, vector<P>, greater<P> > que;
fill(dist1, dist1+MAXV, INF);
fill(dist2, dist2+MAXV, INF);
dist1[] = ;
que.push(P(dist1[], ));
while (!que.empty())
{
P p = que.top();
que.pop();
int v = p.second, d = p.first;
if (dist2[v] < d) continue;//如果次短路径都小于d 那么就不用再继续去更新
int t = head[v];
while (t != -)
{
Edge e = edge[t];
int d2 = e.cost + d;//到e.to的假设次短距离 是到v的最距离 + e.cost
if(d2 < dist1[e.to])//如果次短路小于最短路 交换最短路和次短路
{
swap(dist1[e.to], d2);
que.push(P(dist1[e.to], e.to));
}
if (d2 < dist2[e.to] && d2 > dist1[e.to])//如果可以更新次短路
{
dist2[e.to] = d2;
que.push(P(dist2[e.to], e.to));//这两句if 体现次短路 要么是到达其他某个顶点的最短路加上u->v这条边,要么是到u的次短路再加上u->v这条边
}
t = e.next;
}
}
return dist2[V];
} int main()
{
freopen("in.txt", "r", stdin);
scanf("%d%d", &V, &E);
memset(head, -, sizeof(head));
memset(edge, -, sizeof(edge));
for (int i = ; i < E; i++)
{
int from, to, cost;
scanf("%d%d%d", &from, &to, &cost);
Add(from, to, cost);
Add(to, from, cost);
}
int ans = dijkstra();
//cout << ans << endl;
printf("%d\n", ans);
}

次最短路径 POJ 3255 Roadblocks的更多相关文章

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

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

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

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

  3. POJ 3255 Roadblocks (Dijkstra求最短路径的变形)(Dijkstra求次短路径)

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16425   Accepted: 5797 Descr ...

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

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

  5. poj 3255 Roadblocks

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

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

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

  7. POJ 3255 Roadblocks --次短路径

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

  8. POJ 3255 Roadblocks (次短路 SPFA )

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

  9. POJ 3255 Roadblocks (次短路)

    题意:给定一个图,求一条1-n的次短路. 析:次短路就是最短路再长一点呗,我们可以和求最短路一样,再多维护一个数组,来记录次短路. 代码如下: #pragma comment(linker, &quo ...

随机推荐

  1. SpringBoot 2.x (7):拦截器

    类似以前SpringMVC的拦截器,但也有一些区别 SpringBoot的拦截器有两种方式: 第一种方式:过时的方式,适用于SpringBoot1.x的方式 package org.dreamtech ...

  2. Java入门小知识

    软件开发什么是软件?  一系列按照特定顺序组织的计算机数据和指令的集合什么是开发?  制作软件 人机交互  软件的出现实现了人与计算机之间的更好的交互交互方式   图形化界面:这种方式简单直观,使用者 ...

  3. 全志R58平台的GPIO引脚控制

    全志R58平台的GPIO引脚控制 2017/8/18 15:50 版本:V1.0 开发板:SC5806(全志R58平台) SDK:android4.4.4 本文以GPIO引脚PD24为例,在开发板的背 ...

  4. Android开发中SharedPreferences的使用

    在Android开发中,在储存少量的数据时,个人感觉SharedPreferences是最好的选择,SharedPreferences是以键值对的方式进行储存,支持boolean,int,float, ...

  5. Python3简明教程(十一)—— 类

    本节中将通过定义一些简单的 Python 类,来学习 Python 面向对象编程的基本概念. 定义类 在写你的第一个类之前,你应该知道它的语法.我们以下面这种方式定义类: class nameofth ...

  6. vue 组件名和方法名 重名了,报function错误

    vue 组件名和方法名 重名了,报function错误

  7. docker之启动创建容器流程

    libcontainer的工作流程 execdriver的run方法通过docker daemon提交一份command信息创建了一份可供libcontainer解读的容器配置container,继而 ...

  8. 解决for循环下变量显示一致的问题

    for(var i=0;i<10;i++){ setTimeOut(function(){ console.log("i:",i); },100) } 上面显示的打印出来结果 ...

  9. jquery 定位

    jquery 定位 <html> <head> <title>jquery 定位</title> </head> <body> ...

  10. spring mvc poi excel

    Util类 package com.common.util; public class ExportUtil { private XSSFWorkbook wb = null; private XSS ...