Roadblocks http://poj.org/problem?id=3255
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
#include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
#include"cstdlib"
#include"queue"
#include"vector"
using namespace std;
const int inf=0x7fffffff;
const int ms=;
struct edge
{
//int from;
int to;
int cost;
};
typedef pair<int,int> P;
int main()
{
int i,j,N,R,ans,a,b,w;
scanf("%d%d",&N,&R);
vector<edge> vec[N+];
for(i=;i<=R;i++)
{
scanf("%d%d%d",&a,&b,&w);
edge e;
e.to=b;
e.cost=w;
//vec[a].push_back(edge{b,w}); 有警告
vec[a].push_back(e);
e.to=a;
vec[b].push_back(e);
}
int dist[N+],dist2[N+];
priority_queue<P,vector<P>,greater<P> > que;
fill(dist,dist+N+,inf);
fill(dist2,dist2+N+,inf);
dist[]=;
que.push(P(,));
while(!que.empty())
{
P p=que.top();
que.pop();
int v=p.second;
int d=p.first;
if(dist2[v]<d)
continue;
for(i=;i<vec[v].size();i++)
{
edge &e=vec[v][i];
int d2=d+e.cost;
if(dist[e.to]>d2)
{
swap(dist[e.to],d2);
que.push(P(dist[e.to],e.to));
}
if(dist2[e.to]>d2&&dist[e.to]<d2)
{
dist2[e.to]=d2;
que.push(P(dist2[e.to],e.to));
}
}
}
//printf("%d\n",dist2[N]);
cout<<dist2[N]<<endl;
return ;
}
Roadblocks http://poj.org/problem?id=3255的更多相关文章
- poj 1651 http://poj.org/problem?id=1651
http://poj.org/problem?id=1651Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K To ...
- poj-3056 http://poj.org/problem?id=3056
http://poj.org/problem?id=3056 The Bavarian Beer Party Time Limit: 6000MS Memory Limit: 65536K Tot ...
- poj 1679 http://poj.org/problem?id=1679
http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- POJ3278http://poj.org/problem?id=3278
http://poj.org/problem?id=3278 题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步 #include<stdio.h> #include&l ...
- 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258
#include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...
- http://poj.org/problem?id=3278(bfs)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 76935 Accepted: 24323 ...
- poj 1915 http://poj.org/problem?id=1915
/**< */#include <stdio.h> #include <string.h> #include <stdlib.h> #include < ...
- http://poj.org/problem?id=2253
floyd的应用求每条路径两点之间最大距离的最小值 #include <iostream> #include <cstdio> #include <algorithm&g ...
- 线段树 (区间更新,区间查询) poj http://poj.org/problem?id=3468
题目链接 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
随机推荐
- 关于Tokenizer与TokenFilter的区别
TokenStream是一个能在被调用后产生语汇单元流的类,但是 TokenStream 类有两个不同的类型:Tokenizer 类和 TokenFilter 类.这两个类都是从抽象类TokenStr ...
- Window下开发React-Native Android步骤
1.安装Android开发环境 下载并安装JDK 下载并安装Android SDK, Android NDK 启动SDK下面的SDK Manager.exe,安装相关SDK Platform-tool ...
- Jvm基础(1)-Java运行时数据区
最近在看<深入理解Java虚拟机>,里面讲到了Java运行时数据区,这是Jvm基本知识,把读书笔记记录在此.这些知识属于常识,都能查到的,如果我有理解不对的地方,还请指出. 首先把图贴上来 ...
- POJ 3660 Cow Contest (Floyd)
http://poj.org/problem?id=3660 题目大意:n头牛两两比赛经过m场比赛后能判断名次的有几头可转 化为路径问题,用Floyd将能够到达的路径标记为1,如果一个点能 够到达剩余 ...
- AngularJS 学习笔记二
AngularJS指令 指令 描述 讲解 ng_app 定义应用程序的根元素. 指令 ng_bind 绑定 HTML 元素到应用程序数据. 简介 ng_click 定义元素被单击时的行为. HTML ...
- OAuth 2.0介绍学习
OAuth2.0是OAuth协议的下一版本,但不向后兼容OAuth 1.0即完全废止了OAuth1.0. OAuth 2.0关注客户端开发者的简易性.要么通过组织在资源拥有者和HTTP服务商之间的被批 ...
- 转载.net泛型理解说明
net泛型理解 泛型简介: 泛型(Generic Type)是.NET Framework2.0最强大的功能之一.泛型的主要思想是将算法与数据结构完全分离开,使得一次定义的算法能作用于多种数据结构,从 ...
- [5] Zygote
Android设备中的两大进程,如下图 1,由init进程创建的Daemon进程 2,由 Zygote进程创建的应用程序进程 什么是Zygote? zygote是“受精卵”的意思.在Android里, ...
- Light oj 1234 - Harmonic Number
题目链接:http://lightoj.com/volume_showproblem.php?problem=1234 给你一个数n,让你求 这个要是直接算的话肯定TLE,要是用1e8的数组预处理存储 ...
- Java NIO原理和使用(转载一)
Java NIO非堵塞应用通常适用用在I/O读写等方面,我们知道,系统运行的性能瓶颈通常在I/O读写,包括对端口和文件的操作上,过去,在打开一个I/O通道后,read()将一直等待在端口一边读取字节内 ...