POJ3255次短路
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次短路的更多相关文章
- POJ-3255 Roadblocks---Dijkstra队列优化+次短路
题目链接: https://vjudge.net/problem/POJ-3255 题目大意: 给无向图,求1到n的次短路长度 思路: 由于边数较多,应该使用dijkstra的队列优化 用d数组存储最 ...
- POJ3255 Roadblocks [Dijkstra,次短路]
题目传送门 Roadblocks Description Bessie has moved to a small farm and sometimes enjoys returning to visi ...
- 【POJ3255/洛谷2865】[Usaco2006 Nov]路障Roadblocks(次短路)
题目: POJ3255 洛谷2865 分析: 这道题第一眼看上去有点懵-- 不过既然要求次短路,那估计跟最短路有点关系,所以就拿着优先队列优化的Dijkstra乱搞,搞着搞着就通了. 开两个数组:\( ...
- 算法复习———dijkstra求次短路(poj3255)
题目: Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...
- poj3255 Roadblocks 次短路
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10098 Accepted: 3620 Descr ...
- POJ3255 Roadblocks 严格次短路
题目大意:求图的严格次短路. 方法1: SPFA,同时求单源最短路径和单源次短路径.站在节点u上放松与其向量的v的次短路径时时,先尝试由u的最短路径放松,再尝试由u的次短路径放松(该两步并非非此即彼) ...
- POJ3255(次最短路)
描述 求1到n的次最短路 开个\(dis[maxn][2]\)的储存距离的二维数组,0储存最短路,1储存次短路 初始化全为正无穷,\(dis[1][0]=0;\) 然后遍历更新时,先尝试更新最短路和次 ...
- 《挑战程序设计竞赛》2.5 最短路 AOJ0189 2249 2200 POJ3255 2139 3259 3268(5)
AOJ0189 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0189 题意 求某一办公室到其他办公室的最短距离. 多组输入,n表示 ...
- POJ3255 Roadblocks 【次短路】
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7760 Accepted: 2848 Descri ...
随机推荐
- 使用btoa和atob来进行Base64转码和解码
btoa: 将普通字符串转为Base64字符串 atob: 将Base64字符串转为普通字符串 说明:window.btoa不支持汉字: ===>使用window.encodeURI ...
- jsp base标签与meta标签学习小结
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- php fsockopen
1.PHP fsockopen函数说明: Open Internet or Unix domain socket connection(打开套接字链接) Initiates a socket conn ...
- margin
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 第13章 Swing程序设计----标签组件与图标
在Swing中显示文本或提示信息的方法是使用标签.本节将探讨Swing标签的用法.如何创建标签,以及如何在标签上放置文本和图标. 1.标签的使用 标签可以显示一行只读文本.一个图像或带图像的文本,它并 ...
- chrome下如何显示打开网页的IP地址
Website IP:装上之后在网页右下角能够显示当前访问网页的IP地址,这个对定位哪台前端机是有问题的,特别有帮助.
- Eclipse配置
下载地址:http://www.eclipse.org/downloads/ tomcat plugin:http://www.eclipsetotale.com/tomcatPlugin.html# ...
- 【转载】linux环境下为firefox/chrome浏览器安装flash player
本文转载自 http://blog.sina.com.cn/s/blog_6ad624380102v1xf.html firefox安装flash player的方法: 先到adobe网站上下 ...
- 2016大连网络赛 Different GCD Subarray Query
Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- Java关键字transient和volatile小结(转)
Java关键字transient和volatile小结(转) transient和volatile两个关键字一个用于对象序列化,一个用于线程同步,都是Java中比较高阶的话题,简单总结一下. tran ...