POJ3255Roadblocks[次短路]
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 12697 | Accepted: 4491 |
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 home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.
The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.
The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).
Input
Lines 2..R+1: Each line contains three space-separated integers: A, B, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)
Output
Sample Input
4 4
1 2 100
2 4 200
2 3 250
3 4 100
Sample Output
450
Hint
Source
d[u][0]和d[u][1]分别最短路和次短路
//spfa 32MS NO.1
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int N=,M=,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n,m,u,v,w;
struct edge{
int v,w,ne;
}e[M<<];
int h[N],cnt=;
inline void ins(int u,int v,int w){
cnt++;
e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
}
int d[N][],inq[N],q[N],head=,tail=;
void spfa(){
for(int i=;i<=n;i++){d[i][]=d[i][]=INF;}
d[][]=; inq[]=; q[++tail]=;
while(head<=tail){
int u=q[head++];//printf("u %d\n",u);
inq[u]=;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w;
if(d[v][]>d[u][]+w){
d[v][]=d[v][];
d[v][]=d[u][]+w;
if(!inq[v]){inq[v]=;q[++tail]=v;}
}else if(d[v][]>d[u][]+w&&d[v][]<d[u][]+w){
d[v][]=d[u][]+w;
if(!inq[v]){inq[v]=;q[++tail]=v;}
}
if(d[v][]>d[u][]+w){
d[v][]=d[u][]+w;
if(!inq[v]){inq[v]=;q[++tail]=v;}
}
}
}
}
int main(int argc, const char * argv[]) {
n=read();m=read();
for(int i=;i<=m;i++){u=read();v=read();w=read();ins(u,v,w);}
spfa();
printf("%d",d[n][]);
return ;
}
//dijkstra 63MS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int N=,M=,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n,m,u,v,w;
struct edge{
int v,w,ne;
}e[M<<];
int h[N],cnt=;
inline void ins(int u,int v,int w){
cnt++;
e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
}
int d[N][],vis[N][];
struct hn{
int u,d,p;
hn(int a=,int b=,int c=):u(a),d(b),p(c){}
bool operator < (const hn &rhs)const{return d>rhs.d;}
};
priority_queue<hn> q;
void dijkstra(){
for(int i=;i<=n;i++) {d[i][]=d[i][]=INF;}
q.push(hn(,,));
d[][]=;
while(!q.empty()){
hn now=q.top();q.pop();
int u=now.u,p=now.p;
if(vis[u][p]) continue;
vis[u][p]=;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w;
if(d[v][]>d[u][p]+w){
d[v][]=d[v][];
d[v][]=d[u][p]+w;
q.push(hn(v,d[v][],));
q.push(hn(v,d[v][],));
}else if(d[v][]>d[u][p]+w){
d[v][]=d[u][p]+w;
q.push(hn(v,d[v][],));
}
}
}
}
int main(int argc, const char * argv[]) {
n=read();m=read();
for(int i=;i<=m;i++){u=read();v=read();w=read();ins(u,v,w);}
dijkstra();
printf("%d",d[n][]);
return ;
}
POJ3255Roadblocks[次短路]的更多相关文章
- POJ3255-Roadblocks(最短路)
Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best ...
- POJ-3255-Roadblocks(次短路的另一种求法)
Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. Sh ...
- bzoj1001--最大流转最短路
http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...
- 【USACO 3.2】Sweet Butter(最短路)
题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...
- Sicily 1031: Campus (最短路)
这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...
- 最短路(Floyd)
关于最短的先记下了 Floyd算法: 1.比较精简准确的关于Floyd思想的表达:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设maz ...
- bzoj1266最短路+最小割
本来写了spfa wa了 看到网上有人写Floyd过了 表示不开心 ̄へ ̄ 改成Floyd试试... 还是wa ヾ(。`Д´。)原来是建图错了(样例怎么过的) 结果T了 于是把Floyd改回spfa 还 ...
- HDU2433 BFS最短路
Travel Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 最短路(代码来源于kuangbin和百度)
最短路 最短路有多种算法,常见的有一下几种:Dijstra.Floyd.Bellman-Ford,其中Dijstra和Bellman-Ford还有优化:Dijstra可以用优先队列(或者堆)优化,Be ...
随机推荐
- XSS攻击的解决方法
在我上一篇<前端安全之XSS攻击>文中,并没有把XSS攻击的解决办法说完整,而XSS的攻击又那么五花八门,有没有一招“独孤九剑”能够抗衡,毕竟那么多情况场景,开发人员无法一一照顾过来,而今 ...
- Oracle基础和用户管理
1.数据库的使用: 项目的规模:负载量(用户)有多大? 成本: 安全性: (小型数据库)access.forbase 负载小 :100人以内,比如留言板,信息管理系统. 成本:千元以内. 安全性要 ...
- iOS 判断数组是否为空
有人说可以用([array count]==0 )来判断是否为空,都是坑,如果array为空的话,执行count就会直接报错,程序崩溃退出. 正确判断NSArray是否为空的方法:用 (!array) ...
- SharePoint 2013 重命名网站集名称(SharePoint 2013 rename site collection)
最近使用SharePoint中,遇到一个需要重命名网站集的需求,主要是网站用数据库备份/还原的方式,想要改网站集的地址,然后搜了一下PowerShell: $site = Get-SPSite -Id ...
- JavaScript学习03 JS函数
JavaScript学习03 JS函数 函数就是包裹在花括号中的代码块,前面使用了关键词function: function functionName() { 这里是要执行的代码 } 函数参数 函数的 ...
- 《The Linux Command Line》 读书笔记02 关于命令的命令
<The Linux Command Line> 读书笔记02 关于命令的命令 命令的四种类型 type type—Indicate how a command name is inter ...
- 请各位帮帮忙:Android LBS应用——CityExplorer (v1.0) 调研
Hello哇各位亲!! 请各位帮帮忙:Android LBS应用——CityExplorer(V1.0)调研 嗯,这个事情是这样的,要填一个调查问卷,但是问卷中部分问题是关于这个叫做CityExplo ...
- iOS音频开发之`AudioStreamBasicDescription`
这个类提供了对于音频文件的描述 An audio stream is a continuous series of data that represents a sound, such as a so ...
- swift 2.2 语法 (上)
前言: 1.此文中的语法会根据Swift的升级变动而更新. 2.如果需要请移步 -> swift2.2 语法(中).swift 2.2语法(下) Swift与OC中常见的区别 导入框架 OC: ...
- git入门学习(二):新建分支/上传代码/删除分支
一.git新建分支,上传代码到新的不同分支 我要实现的效果,即是多个内容的平行分支:这样做的主要目的是方便统一管理属于同一个内容的不同的项目,互不干扰.如图所示: 前提是我的github上已经有we ...