Roadblocks(POJ 3255)
- 原题如下:
Roadblocks
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19314 Accepted: 6777 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
Line 1: Two space-separated integers: N and R
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
Line 1: The length of the second shortest path between node 1 and node NSample Input
4 4
1 2 100
2 4 200
2 3 250
3 4 100Sample Output
450
Hint
Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450) - 题解:这题要求的是次短路,根据Dijkstra算法的思路,依次确定尚未确定的顶点中距离最小的顶点可求得最短路,在这个基础上做少许修改,即可求得次短路,到某个顶点v的次短路无非两种情况:①到其它顶点u的最短路再加上u→v的边,②到u的次短路再加上u→v的边。因此,只要求出到所有顶点的最短路和次短路就好了。对于每个顶点,我们记录最短路和次短路,类似Dijkstra算法那样,不断更新这两个距离。(注:这道题中男女之间的二分图结构是无用的陷阱条件,许多问题中,如果有特殊的结构,往往会考虑如何利用这个结构,但这题不是的)
- 代码:
#include <cstdio>
#include <queue>
#include <vector>
#include <functional>
#include <cctype>
#include <algorithm>
#define num s-'0' using namespace std;
typedef pair<int, int> P; struct edge
{
int to;
int cost;
}; const int MAX_V=;
const int INF=0x3f3f3f3f;
int V,E,n;
vector<edge> G[MAX_V];
int dist[MAX_V];
int dist2[MAX_V]; void solve(); void swap(int &x, int &y)
{
int t=x;
x=y;y=t;
} void read(int &x){
char s;
x=;
bool flag=;
while(!isdigit(s=getchar()))
(s=='-')&&(flag=true);
for(x=num;isdigit(s=getchar());x=x*+num);
(flag)&&(x=-x);
} void write(int x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
write(x/);
putchar(x%+'');
} int main()
{
read(V);read(E);
for (int i=; i<E; i++)
{
edge e;
int s,t,c;
read(s);read(t);read(c);
e.to=t-;e.cost=c;
G[s-].push_back(e);
e.to=s-;e.cost=c;
G[t-].push_back(e);
}
solve();
write(dist2[V-]);
putchar('\n');
} void solve()
{
priority_queue<P, vector<P>, greater<P> > que;
fill(dist, dist+V, INF);
fill(dist2, dist2+V, INF);
dist[]=;
que.push(P(,));
while (!que.empty())
{
P p=que.top(); que.pop();
int v=p.second, d=p.first;
if (dist2[v]<d) continue;
for (int i=; i<G[v].size(); i++)
{
edge &e=G[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));
}
}
}
}
Roadblocks(POJ 3255)的更多相关文章
- MST:Roadblocks(POJ 3255)
路上的石头 题目大意:某个街区有R条路,N个路口,道路双向,问你从开始(1)到N路口的次短路经长度,同一条边可以经过多次. 这一题相当有意思,现在不是要你找最短路径,而是要你找次短路经,而且次短 ...
- POJ 3255 Roadblocks(A*求次短路)
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12167 Accepted: 4300 Descr ...
- POJ 3255 Roadblocks (次级短路问题)
解决方案有许多美丽的地方.让我们跳回到到达终点跳回(例如有两点)....无论如何,这不是最短路,但它并不重要.算法能给出正确的结果 思考:而最短的路到同一点例程.spfa先正达恳求一次,求的最短路径的 ...
- 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 ...
- poj 3255 Roadblocks 次短路(两次dijksta)
Roadblocks Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...
- POJ 3255 Roadblocks (Dijkstra求最短路径的变形)(Dijkstra求次短路径)
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16425 Accepted: 5797 Descr ...
- 【POJ - 3255】Roadblocks(次短路 Dijkstra算法)
Roadblocks 直接翻译了 Descriptions Bessie搬到了一个新的农场,有时候他会回去看他的老朋友.但是他不想很快的回去,他喜欢欣赏沿途的风景,所以他会选择次短路,因为她知道一定有 ...
随机推荐
- TCP/IP速记
目录 网络协议 OSI七层模型和TCP/IP五层模型 TCP/IP五层模型 TCP的三次握手和四次挥手 三次握手进行连接 四次挥手断开连接 TCP连接的特点 TCP是如何保证安全可靠的 UDP连接的特 ...
- MongoDB学习4:MongoDB复制集机制和原理,搭建复制集
1.复制集的作用 1.1 MongoDB复制集的主要意义在于实现服务高可用 1.2 它的实现依赖于两个方面的功能: · 数据写入时将数据迅速复制到另一个独立节点上 · 在接收写入的 ...
- js获取当前地址信息
var url = window.location.href; //获取当前窗口的Url; 结果:http://localhost:61768/Home/Index?id=2&age=18 v ...
- 记一次mysql数据库被勒索(上)
家里搞了台旧电脑做NAS,安装了nextcloud,选择了mysql做为数据库. 当时也没有想太多,mysql数据库密码随便设置了个123456,用的一切正常. 然后,听说可以找电信申请换个公网IP的 ...
- vue watch/ computed的应用(做一个简单的父子之间的传递/电话号码的搜索)
父组件中当点击搜索的时候请求接口,然后把新的数据用 computed 传递给子组件 <van-search v-model="onSeachPhone" show-actio ...
- Android 使用AS编译出错:Error: Duplicate resources
原因在于drawable目录下有重复的文件名!!! 修改其中的一个文件名或者删除其中一张图(或者xml文件)就可以啦~
- 正则表达式截取xml
$str = '<Ips><GateWayRsp><head><ReferenceID>123</ReferenceID><RspCo ...
- 【小白学AI】XGBoost推导详解与牛顿法
文章来自微信公众号:[机器学习炼丹术] 目录 1 作者前言 2 树模型概述 3 XGB vs GBDT 3.1 区别1:自带正则项 3.2 区别2:有二阶导数信息 3.3 区别3:列抽样 4 XGB为 ...
- 这可能是最详细的解析HTTP走私攻击的文章
前言 HTTP Desync Attacks也就是HTTP走私攻击,是我见到的比较有趣的一种攻击方式,这里来对这种漏洞进行介绍. TL;DR HTTP走私攻击利用了HTTP协议本身的问题:HTTP中存 ...
- Shell编程—正则表达式
1什么是正则表达式 1.1定义 正则表达式是你所定义的模式模板,Linux工具可以用它来过滤文本.Linux 工具(比如sed编辑器或gawk程序)能够在处理数据时使用正则表达式对数据进行模式匹配. ...