poj 3255 求次大最短路
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5508 | Accepted: 2088 |
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
N
Sample Input
4 4
1 2 100
2 4 200
2 3 250
3 4 100
Sample Output
450
Hint
Source
题目大意:在一个图上有许多个农场,有个人从1农场出发,到他的朋友n农场去,他不想走一条最短路径,这次他想换条路走,要你帮他找一条次短路径,次短路的定义是,比最短路径长度短(可能有多条),但是不会比其他的路径长度长。而且告诉你数据中一定存在至少一条次短路。
思路 :暴力每一条边
求出从起点到达该边的起点的最短距离 求终点到达该边的终点距离 相加再加上边长 判断是否比最短路长 且最接近最短路
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<vector>
#include<string.h>
using namespace std;
const int INF=10000000;
const int maxn=5002;
struct Edge{
int from,to,dis;
};
struct node{
int d,u;
bool operator <(const node &rhs) const {
return d>rhs.d;//从小到大排列
}
};
int mn;
struct Dij
{
int n,m;//点数和边数
vector<Edge>edges;//边列表
vector<int>nd[maxn];//从每个节点出发的边编号 从0开始
bool vis[maxn];//是否参观过
int mmin[maxn];//s到各个点的最小距离
void init(int n)
{
this->n=n;
for(int i=0;i<n;i++) nd[i].clear();//邻接表清空
edges.clear();//清空边表
} void addedge(int from,int to,int dis)
//如果是无向图 每条无向边需要调用2次
{
edges.push_back((Edge){from,to,dis});//是{ 不是(
m=edges.size();
nd[from].push_back(m-1);
} void dij(int s)//求s到所有点的距离
{
priority_queue<node>que;
for(int i=0;i<n;i++) mmin[i]=INF;
mmin[s]=0;
memset(vis,0,sizeof(vis));
que.push((node){0,s});//注意符号 不是括号
while(!que.empty())
{
node x=que.top();que.pop();
int u=x.u;
if(vis[u]) continue;
vis[u]=true;
for(int i=0;i<nd[u].size();i++)
{
Edge& e=edges[nd[u][i]];
int max1=0,max2=0;
if(mmin[e.to]>mmin[u]+e.dis)
{
// printf("mmin[e.to]=%d\n",mmin[e.to]);
max1=mmin[e.to];
mmin[e.to]=mmin[u]+e.dis;
max2=mmin[e.to];
if(max1!=INF&&max2!=INF&&mn>max1-max2) mn=max1-max2;
que.push((node){mmin[e.to],e.to});
} }
}
}
}; vector<int>path;
int main()
{
int n,m,x,y,z,s,e;
while(scanf("%d %d",&n,&m)!=EOF)
{
mn=999999999;
s=0;e=n-1;
Dij solve[2];
solve[0].init(n);
solve[1].init(n);
while(m--)
{
scanf("%d %d %d",&x,&y,&z);
x--;y--;
solve[0].addedge(x,y,z);solve[0].addedge(y,x,z);
solve[1].addedge(x,y,z);solve[1].addedge(y,x,z);
}
solve[0].dij(s);//求1到所有点的距离
solve[1].dij(e);//求n到所有点的距离
int ans=INF;
for(int i=0;i<solve[0].edges.size();i++)
{
Edge &edge=solve[0].edges[i];
int st=edge.from,ed=edge.to;
int mid=solve[0].mmin[st]+solve[1].mmin[ed]+edge.dis;
if(mid>solve[0].mmin[e])
{
if(ans>mid) ans=mid;
}
}
printf("%d\n",ans);
}
return 0;
}
参考:
http://blog.csdn.net/wangjian8006/article/details/7992280
poj 3255 求次大最短路的更多相关文章
- POJ 3255:Roadblocks(次短路)
题目大意:求无向图的次短路. 分析: 在起点终点各求一次最短路,枚举边,通过该边的最短路为其权值加上到起点和终点最短路之和,找到最短但又比最短路长的路径. 代码: program block; typ ...
- 【POJ - 3255】Roadblocks(次短路 Dijkstra算法)
Roadblocks 直接翻译了 Descriptions Bessie搬到了一个新的农场,有时候他会回去看他的老朋友.但是他不想很快的回去,他喜欢欣赏沿途的风景,所以他会选择次短路,因为她知道一定有 ...
- POJ 3255 Roadblocks(A*求次短路)
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12167 Accepted: 4300 Descr ...
- POJ 3255 Roadblocks (次级短路问题)
解决方案有许多美丽的地方.让我们跳回到到达终点跳回(例如有两点)....无论如何,这不是最短路,但它并不重要.算法能给出正确的结果 思考:而最短的路到同一点例程.spfa先正达恳求一次,求的最短路径的 ...
- Remmarguts' Date POJ - 2449 (A*搜索|k短路)
"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. ...
- poj 1847 Tram【spfa最短路】
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12005 Accepted: 4365 Description ...
- POJ - 1062 昂贵的聘礼(最短路Dijkstra)
昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u SubmitStatus Descr ...
- POJ 3255 Roadblocks (次短路模板)
Roadblocks http://poj.org/problem?id=3255 Time Limit: 2000MS Memory Limit: 65536K Descriptio ...
- POJ - 3255 SPFA+邻接表求次短路径
题意:给出m条边 , n个顶点,u [ i ]到v [ i ] 的距离w [ i ],求除了最短路的那条最短的边的长度. 思路:之前有做过相似的题,使用迪杰斯特拉算法求单源最短路径,并且记录路径,枚举 ...
随机推荐
- UESTC_神秘绑架案 CDOJ 881
神秘绑架案 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Sta ...
- UESTC_秋实大哥打游戏 2015 UESTC Training for Data Structures<Problem H>
H - 秋实大哥打游戏 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- 数字图像处理之sobel边缘检测
在前两部文章介绍了几种边缘检测算法,和位图的内存结构.如果对前两篇文章已经理解透彻 了,那么本文将带你进入数字图像处理的世界. 本文通过C代码实现基本的sobel边缘检测,包括8个方向和垂直方向: 代 ...
- 【错误】:MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'
错误:MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 解决 ...
- log4net 使用与配置 每天一份log文件
1.下载 或 在nuget安装 log4net 2. web.config (app.config) <configuration> <configSections> < ...
- 【v2.x OGE教程 20】粒子效果
1.介绍 粒子系统表示三维计算机图形学中模拟一些特定的模糊现象的技术.而这些现象用其他传统的渲染技术难以实现的真实感的 game physics.常常使用粒子系统模拟的现象有火.爆炸.烟.水流.火花. ...
- java基础之集合List-ArrayList、LinkedList、Vector的差别
PS:本篇博客主要參考jdk的底层源代码.而非自己动手写代码. 请问ArrayList.LinkedList.Vector的差别 ①ArrayList底层实际上是採用数组实现的(而且该数组的类型的Ob ...
- C语言头文件组织
一.全局变量单独编写(很值得借鉴). 一般习惯将不同功能模块放到一个头文件和一个C文件中. 例如是写一些数学计算函数: //mymath.h #ifndef _mymath_H #define _my ...
- 【取对数+科学计数法】【HDU1060】 N^N
Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- iOS中的下载管理器(支持断点续传)
在空闲时间自己编写了一个简单的iOS下载管理器.该管理器实现如下功能: 1.能够支持正常的下载,暂停,继续操作. 2.支持断点续传,实现暂停执行继续操作后,依然能正常将文件下载完成. 3.实现实时状态 ...