POJ3255

题意:给定一个图,求从1到n的次短路

分析:我们需要在dijkstra上作出一些修改,首先,到某个顶点v的次短路要么是到其他某个顶点u的最短路在加上u到v的边,要么是到v的次短路再加上u到v的边,因此我们需要记录的是最短和次短路。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
const int maxn=;
const int INF=<<;
struct edge
{
int to,cost; //顶点号和距离
};
typedef pair<int,int> P; //first是最短距离,second是顶点编号
vector<edge> g[maxn];
int n,r; //顶点数,边数
int d[maxn]; //最短距离
int d2[maxn]; //次短距离
void dijkstra(int s)
{
priority_queue<P, vector<P>, greater<P> > que;
fill(d,d++n,INF);
fill(d2,d2++n,INF);
d[s]=;
que.push(P(,s)); while(!que.empty()){
P p=que.top(); que.pop();
int v=p.second,t=p.first;
if(d2[v]<t) continue;
for(int i=;i<g[v].size();i++){
edge e=g[v][i];
int dist=e.cost+t;
if(d[e.to]>dist){
swap(d[e.to],dist);
que.push(P(d[e.to],e.to));
}
if(d2[e.to]>dist&&d[e.to]<dist){
d2[e.to]=dist;
que.push(P(d2[e.to],e.to));
}
}
}
}
int main()
{
while(cin>>n>>r)
{
for(int i=;i<r;i++)
{
int x,y,num;
scanf("%d%d%d",&x,&y,&num);
//无向图建图
edge e1;
e1.to=y,e1.cost=num;
g[x].push_back(e1);
edge e2;
e2.to=x,e2.cost=num;
g[y].push_back(e2);
}
dijkstra();
cout<<d2[n]<<endl;
}
return ;
}

POJ3255次短路的更多相关文章

  1. POJ-3255 Roadblocks---Dijkstra队列优化+次短路

    题目链接: https://vjudge.net/problem/POJ-3255 题目大意: 给无向图,求1到n的次短路长度 思路: 由于边数较多,应该使用dijkstra的队列优化 用d数组存储最 ...

  2. POJ3255 Roadblocks [Dijkstra,次短路]

    题目传送门 Roadblocks Description Bessie has moved to a small farm and sometimes enjoys returning to visi ...

  3. 【POJ3255/洛谷2865】[Usaco2006 Nov]路障Roadblocks(次短路)

    题目: POJ3255 洛谷2865 分析: 这道题第一眼看上去有点懵-- 不过既然要求次短路,那估计跟最短路有点关系,所以就拿着优先队列优化的Dijkstra乱搞,搞着搞着就通了. 开两个数组:\( ...

  4. 算法复习———dijkstra求次短路(poj3255)

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

  5. poj3255 Roadblocks 次短路

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10098   Accepted: 3620 Descr ...

  6. POJ3255 Roadblocks 严格次短路

    题目大意:求图的严格次短路. 方法1: SPFA,同时求单源最短路径和单源次短路径.站在节点u上放松与其向量的v的次短路径时时,先尝试由u的最短路径放松,再尝试由u的次短路径放松(该两步并非非此即彼) ...

  7. POJ3255(次最短路)

    描述 求1到n的次最短路 开个\(dis[maxn][2]\)的储存距离的二维数组,0储存最短路,1储存次短路 初始化全为正无穷,\(dis[1][0]=0;\) 然后遍历更新时,先尝试更新最短路和次 ...

  8. 《挑战程序设计竞赛》2.5 最短路 AOJ0189 2249 2200 POJ3255 2139 3259 3268(5)

    AOJ0189 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0189 题意 求某一办公室到其他办公室的最短距离. 多组输入,n表示 ...

  9. POJ3255 Roadblocks 【次短路】

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7760   Accepted: 2848 Descri ...

随机推荐

  1. java获取程序执行时间

    第一种是以毫秒为单位计算的. //伪代码 long startTime=System.currentTimeMillis(); //获取开始时间 doSomeThing(); //测试的代码段 lon ...

  2. perfect-scrollbar示例

    <!DOCTYPE html> <html> <head> <title>perfect-scrollbar - www.97zzw.com -97站长 ...

  3. layer弹出层

    最近因为项目要求做了一个layer弹出层demo,先看效果图 好了,现在开始上代码 index.jsp <%@ page language="java" import=&qu ...

  4. LightOJ 1370 Bi-shoe and Phi-shoe 数论

    题目大意:f(x)=n 代表1-x中与x互质的数字的个数.给出n个数字a[i],要求f(x)=a[i],求x的和. 思路:每个素数x 有x-1个不大于x的互质数.则f(x)=a[i],若a[i]+1为 ...

  5. Linux 安全模块

    LSM是Linux Secrity Module的简称,即linux安全模块.其是一种轻量级通用访问控制框架,适合于多种访问控制模型在它上面以内核可加载模块的形实现.用户可以根据自己的需求选择合适的安 ...

  6. Loadrunner之文件的下载(八)

    老猪提供: https://mp.weixin.qq.com/s?__biz=MzIwOTMzNDEwNw==&mid=100000013&idx=1&sn=624f5bc74 ...

  7. 在Activity之间使用Intent传值和Bundle传值的区别和方式

    两者本质上没有任何区别.Bundle只是一个信息的载体 将内部的内容以键值对组织 Intent负责Activity之间的交互 自己是带有一个Bundle的Intent.putExtras(Bundle ...

  8. Disassembly3:variable

    Initializer C++ Primer上说:如果未初始化的Built-in type是定义在function外部的,那么它将自动被初始化为“0”:如果uninitialized的built-in ...

  9. 用TcpClient如何获取远程网页的内容

    用TcpClient如何获取远程网页的内容 private string GetHTMLTCP(string URL) { string strHTML = "";//用来保存获得 ...

  10. new关键字的理解-问题型

    //输出的结果是?????????//The answer is good and gbc public class Example { String str=new String("goo ...