POJ 3255
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 6605 | Accepted: 2458 |
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <utility> using namespace std; typedef pair<int,int> pii; const int INF = 1e9;
const int MAX_V = ;
const int MAX_E = ;
int N,R;
int first[MAX_V],next[ * MAX_E],v[ * MAX_E],w[ * MAX_E];
int dist[MAX_V],dist2[MAX_V]; void addedge(int a,int b,int id) {
int e = first[a];
next[id] = e;
first[a] = id;
} void solve() {
fill(dist + ,dist + N + ,INF);
fill(dist2 + ,dist2 + N + ,INF); dist[] = ;
priority_queue<pii,vector<pii>,greater<pii> > q;
q.push(pii(,)); while(!q.empty()) {
pii x = q.top(); q.pop();
int d = x.first,u = x.second;
for (int e = first[u]; e != -; e = next[e]) {
int d2 = d + w[e];
if(dist[v[e]] > d + w[e]) {
dist[v[e]] = d + w[e];
q.push(pii(dist[ v[e] ],v[e]));
} if(d2 < dist2[ v[e] ] && d2 > dist[ v[e] ]) {
dist2[ v[e] ] = d2;
q.push(pii(d2,v[e]));
} }
} printf("%d\n",dist2[N]);
}
int main()
{
//freopen("sw.in","r",stdin); scanf("%d%d",&N,&R);
for (int i = ; i <= N; ++i) first[i] = -;
for (int i = ; i < * R; i += ) {
int u;
scanf("%d%d%d",&u,&v[i],&w[i]);
v[i + ] = u;
w[i + ] = w[i];
addedge(u,v[i],i);
addedge(v[i],u,i + );
} solve(); return ;
}
POJ 3255的更多相关文章
- POJ 3255 Roadblocks(A*求次短路)
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12167 Accepted: 4300 Descr ...
- POJ 3255 Roadblocks (次级短路问题)
解决方案有许多美丽的地方.让我们跳回到到达终点跳回(例如有两点)....无论如何,这不是最短路,但它并不重要.算法能给出正确的结果 思考:而最短的路到同一点例程.spfa先正达恳求一次,求的最短路径的 ...
- poj 3255(次短路)
题目链接:http://poj.org/bbs?problem_id=3255 思路:分别以源点1和终点N为源点,两次SPFA求得dist1[i](1到各点的最短距离)以及dist2[i](各点到N的 ...
- POJ 3255 Roadblocks (次短路模板)
Roadblocks http://poj.org/problem?id=3255 Time Limit: 2000MS Memory Limit: 65536K Descriptio ...
- 次最短路径 POJ 3255 Roadblocks
http://poj.org/problem?id=3255 这道题还是有点难度 要对最短路径的算法非常的了解 明晰 那么做适当的修改 就可以 关键之处 次短的路径: 设u 到 v的边权重为cost ...
- poj 3255 Roadblocks
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13216 Accepted: 4660 Descripti ...
- Roadblocks(poj 3255)
题意:给出n个点,m条双向边,求严格次短路. /* 先spfa预处理出起点到每个点的和每个点到终点的最短距离,然后枚举每条边(这条边必须走),计算此时的最短路径,得出严格次短路. 正确性:因为对于一条 ...
- POJ 3255 Roadblocks --次短路径
由于次短路一定存在,则可知次短路一定是最短路中某一条边不走,然后回到最短路,而且只是一条边,两条边以上不走的话,就一定不会是次短路了(即以边换边才能使最小).所以可以枚举每一条边,算出从起点到这条边起 ...
- MST:Roadblocks(POJ 3255)
路上的石头 题目大意:某个街区有R条路,N个路口,道路双向,问你从开始(1)到N路口的次短路经长度,同一条边可以经过多次. 这一题相当有意思,现在不是要你找最短路径,而是要你找次短路经,而且次短 ...
随机推荐
- 装黑苹果的那些事儿(以ThinkpadE540为例)
苹果系统,有着比window更好的安全性和方便性,更重要的事,没有MAC系统环境,进行iOS开发,是很麻烦的,对新手来说,是很懊恼的一件事.但是白苹果像件奢侈品,吾等常人,很难有经济消费.如是黑苹果是 ...
- Apache+tomcat集群配置
一.软件准备 Apache 2.2 : http://httpd.apache.org/download.cgi,下载msi安装程序,选择no ssl版本 Tomcat 6.0 : http://to ...
- 基于jquery的页面代码的优化
高亮显示,选中的文字链接 显示效果如下 默认选择“通知公告”效果 通知公告 学院动态 行业动态 选择“学院动态”效果 通知公告 学院动态 行业动态 选择“行业动态”效果 通知公 ...
- Enyim.Caching 客户端配置及示例
一.工作准备 memcached客户端:Enyim.Caching.2.13 memcached服务器:memcached-win64-1.4.4-14 备注:不建议使用windows服务器,开发环境 ...
- ASP.NET MVC中使用事务写法
一些项目中,会涉及到事务的写法,比如订单相关,订单成功,会涉及到产品的库存和账户金额的一些信息变动,当然,如果整个流程成功,那是没什么问题,关键是如果中间某一步骤出现bug了,那之前已执行的一些变动就 ...
- [转]ASP.NET MVC Spring.NET NHibernate 整合
请注明转载地址:http://www.cnblogs.com/arhat 在整合这三个技术之前,首先得说明一下整合的步骤,俗话说汗要一口一口吃,事要一件一件做.同理这个三个技术也是.那么在整合之前,需 ...
- IIS 404.17 错误解决方案
操作方法:在管理员身份打开命令行,运行以下命令: C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -i
- 读:HIS 与医保系统的接入方案及实现
HIS 与医保系统的接入方案及实现刘剑锋 李刚荣第三军医大学西南医院信息科(重庆 400038) 医院HIS和医保系统的接口设计方案涉及两个部分,分别由医院和医保中心分别完成相,应的程序设计,这两部分 ...
- VBS数组函数学习实例分析
Array 函数 返回包含数组的Variant. Array(arglist) 参数:arglist是赋给包含在Variant中的数组元素的值的列表(用逗号分隔).如果没有指定此参数,则将会创建零长度 ...
- ASP .NET下的301重定向如何做
using System; using System.Collections.Generic; using System.Text; using System.Web.UI; using System ...