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> # ...
随机推荐
- ACCESS TOKEN
Access Token 在微信公众平台接口开发中,Access Token占据了一个很重要的地位,相当于进入各种接口的钥匙,拿到这个钥匙才有调用其他各种特殊接口的权限. access_token是公 ...
- Maven管理多模块项目
首先,我们要明确的多模块项目的含义,它是指一个应用中包含多个module.一般来说,一个应用单独部署成服务,只是打包的时候,maven会把各个module组合在一起.各模块一般单独打成jar放到lib ...
- struts2实现文件上传
Struts2中实现简单的文件上传功能: 第一步:将如下文件引入到WEB_INF/lib目录下面,对应的jar文件可自行下载 第二步:在包test.struts2下建立类UploadFile pack ...
- ubuntu使用问题与解决记录[持续更新]
1. 添加到计划任务 为脚本增加可执行权限 sudo chmod +x yeelink.sh 将脚本加入cronjob(计划任务) sudo crontab -e 在cornjob文件中添加下面一行, ...
- 从Count看Oracle执行计划的选择
一. 前言 在调查一个性能问题的时候,一个同事问道,为什么数据库有些时候这么不聪明,明明表上有索引,但是在执行一个简单的count的时候居然全表扫描了!难道不知道走索引更快么? 试图从最简单的coun ...
- 第二百六十七天 how can I 坚持
晚上有点小郁闷,小纠结,感觉不应该买房,不知道什么吸引着我一定要买呢,曾经坚持不买房的我,为什么成了这个样子. 搞不懂啊. 元旦就要就看房了, 如果真的要买了,明年的压力就会很大了. 经济到底会成为啥 ...
- 第二百六十四天 how can I 坚持
现在上班闲的有点蛋疼,感觉没什么事,学不到什么东西. 到底要不要买房啊.也想买啊.愁人. 这辈子绝不会就这样. 睡觉.
- 从一个开发的角度看负载均衡和LVS(转)
原文:http://blog.hesey.net/2013/02/introduce-to-load-balance-and-lvs-briefly.html 在大规模互联网应用中,负载均衡设备是必不 ...
- OC内存管理基础
OC 内存管理基础 一. retain和release基本使用 使用注意: 1.你想使用(占用)某个对象,就应该让对象的计数器+1(让对象做一次retain操作) 2.你不想再使用(占用)某个对象,就 ...
- flash的dragonbone插件导入cocos2d的注意事项
一:Flash版本号应该为CS 6.0,低版本号不提供支持 二:新建flash项目的时候应该选择ActionScript3.0 三:动画中仅仅有两种元素,一个是"元件",还有一个是 ...