poj-3255-Roadblocks (求到源点的次短路,Dijkstra改进)

来源:互联网 发布:知乎市场营销活动策划 编辑:程序博客网 时间:2024/04/29 17:44

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 N
Sample Input
4 4
1 2 100
2 4 200
2 3 250
3 4 100
Sample Output
450
Hint
Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

题意就是给你N个点,R条边,然后求出1到N的次短路的路程,这里一条边可以用两次,而且也可以和最短路的路径重复

思路,比如到V这个点的次短路,一定就是到与他连接的一个u的点的最短路,再加上u到v这条边,或者是到u的次短路,再加上v这条边,注意这里u是指到V的一源点,不是具体的某个点,故这里我是参考了《挑战程序设计第二版》这本书,对Dijkstra进行改进,由于是稠密图,故用邻接表存图,并且用堆来进行优化。

完整代码如下:

#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<vector>#include<queue>#include<algorithm>#define MAX_V 5010#define MAx_E 100010using namespace std;int V,E;const int INF = 99999999;int first_short[MAX_V],second_short[MAX_V];//代表最短距离和次短距离 typedef pair<int,int> P;//first代表到该点距离,second代表该点编号,这里也可用结构体代替 struct Edge{int to,cost;}; vector<Edge>G[MAX_V];void Dijkstra(int a){    priority_queue<P,vector<P>,greater<P> >q;    fill(first_short+1,first_short+1+V,INF);    fill(second_short+1,second_short+1+V,INF);    first_short[1] = 0;    q.push(P(0,1));    while(!q.empty()){        P p = q.top();q.pop();        int v = p.second;        int d = p.first;        if(second_short[v] < d) continue;//这一句话也可以去掉,因为放入堆中的最长也是次短路的距离         for(int i=0;i<G[v].size();i++){            Edge e = G[v][i];            int d2 = d + e.cost;//d2代表走这条边的情况             if(first_short[e.to] > d2){//如果走这条边后到e.to这个点的距离比最短路还小,就交换他们                 swap(first_short[e.to],d2);                q.push(P(first_short[e.to],e.to));            }            if(second_short[e.to] > d2 && d2 > first_short[e.to]){//如果d2比最短小,比次短路长,就更新次短路的值                 second_short[e.to] = d2;                q.push(P(second_short[e.to],e.to));//这里次短路也要放入堆中,思考思路中的前几句话。             }        }    }}int main(void){    while(~scanf("%d %d",&V,&E)){        Edge e;        int a,b,c;        for(int i=1;i<=V;i++)            G[i].clear();        for(int i=1;i<=E;i++){//由于是无向图,故这里要存两次             scanf("%d %d %d",&a,&b,&c);            e.to = b,e.cost = c;            G[a].push_back(e);            e.to = a,e.cost = c;            G[b].push_back(e);        }        Dijkstra(1);        printf("%d\n",second_short[V]);    }    return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 下体长毛了很多肉芽很痒怎么办 对方拟把假离婚协议弄假成真怎么办 领主之塔改版后旧材料怎么办 汽车遥控钥匙打不开车门怎么办 苹果手机访问限制密码忘了怎么办 苹果的访问限制密码忘了怎么办 手机下雨天进水声音出了问题怎么办 胸太小了该怎么办经常揉胸会变大吗 鼻冲洗器冲鼻子耳朵进水怎么办 手表进水了然后里面有水珠怎么办 玩游戏后头晕恶心想吐怎么办 吃完美林3小时后又发烧怎么办 颜值和身材都是负分怎么办 妈妈离婚又有一个孩子我该怎么办 孩子妈妈天天给孩子吃药我该怎么办 被像蚂蚁有翅膀的虫子咬了怎么办 1周半宝宝扭脚丫肿了怎么办 离婚后前夫带孩子走了找不到怎么办 宜昌全视之眼逃出卢浮宫怎么办 qq号被盗了密保手机也被改怎么办 乐视盒子控播平台认证怎么办 购买冲气娃娃被发现了怎么办 买了充气娃娃太美舍不得扔怎么办 一品官老爷账号密码忘了怎么办 苹果手机加声音显示出耳机怎么办 被删除的照片恢复后效果变差怎么办 w10系统玩刺激战场声音小怎么办 宝宝不咳嗽但是喉咙有痰怎么办 深圳限行如果车堵在路上怎么办 奔跑吧qq中奖我填写资料怎么办 微信之前绑定的手机号丢了怎么办 龙之谷手游换装备洗炼材料怎么办 小学生在班上碰到流氓同学怎么办 问道手游仓库密码忘了怎么办 问道手游安全码忘了怎么办 问道手游账号密码忘了怎么办 坐一天一夜长途车腰疼怎么办 智联的简历一直说不完整怎么办 智联招聘简历投错了怎么办 投简历的公司写错面试时怎么办 从原单位辞职后档案一直没提怎么办