poj3255

题目:

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: AB, 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)
思路:就是一道单纯的求次短路的题目,先来看如何求解次短路:如果我们要求解起点s到终点t的次短路,那么有两种可能的情况:
(1)起点s到某个顶点u的最短路+(u到t的距离)
(2)起点到某个顶点u的次短路+(u到t的距离)
因此,对于每个结点,我们记录的不仅仅是最短距离,还有次短距离,然后用类似于Dijkstra算法不断更新这两个距离即可求出次短路。
 
代码:
  1 #include <map>
2 #include <set>
3 #include <list>
4 #include <stack>
5 #include <queue>
6 #include <deque>
7 #include <cmath>
8 #include <ctime>
9 #include <string>
10 #include <limits>
11 #include <cstdio>
12 #include <vector>
13 #include <iomanip>
14 #include <cstdlib>
15 #include <cstring>
16 #include <istream>
17 #include <iostream>
18 #include <algorithm>
19 #define ci cin
20 #define co cout
21 #define el endl
22 #define Scc(c) scanf("%c",&c)
23 #define Scs(s) scanf("%s",s)
24 #define Sci(x) scanf("%d",&x)
25 #define Sci2(x, y) scanf("%d%d",&x,&y)
26 #define Sci3(x, y, z) scanf("%d%d%d",&x,&y,&z)
27 #define Scl(x) scanf("%I64d",&x)
28 #define Scl2(x, y) scanf("%I64d%I64d",&x,&y)
29 #define Scl3(x, y, z) scanf("%I64d%I64d%I64d",&x,&y,&z)
30 #define Pri(x) printf("%d\n",x)
31 #define Prl(x) printf("%I64d\n",x)
32 #define Prc(c) printf("%c\n",c)
33 #define Prs(s) printf("%s\n",s)
34 #define For(i,x,y) for(int i=x;i<y;i++)
35 #define For_(i,x,y) for(int i=x;i<=y;i++)
36 #define FFor(i,x,y) for(int i=x;i>y;i--)
37 #define FFor_(i,x,y) for(int i=x;i>=y;i--)
38 #define Mem(f, x) memset(f,x,sizeof(f))
39 #define LL long long
40 #define ULL unsigned long long
41 #define MAXSIZE 100005
42 #define INF 0x3f3f3f3f
43
44 const int mod=1e9+7;
45 const double PI = acos(-1.0);
46
47
48 using namespace std;
49
50 typedef pair<int,int>pii;
51 struct edge
52 {
53 int to,w;
54 edge(int x,int y)
55 {
56 to=x;
57 w=y;
58 }
59 };
60 vector<edge>G[MAXSIZE];//邻接表储存
61 int dis[MAXSIZE];//最短
62 int dis2[MAXSIZE];//次短
63
64 int n,r;
65 void solve()
66 {
67 priority_queue<pii,vector<pii>,greater<pii> >q;//注意这个队列first存的是到每点的最短距离,second存的这个点
68 Mem(dis,INF);
69 Mem(dis2,INF);
70 q.push(pii(0,1));
71 dis[1]=0;
72 while(!q.empty())
73 {
74 pii p=q.top();
75 q.pop();
76 int v=p.second,d=p.first;
77 if(dis2[v]<d)
78 continue;//到v的距离比次短路短(肯定也比最短路短),终止本次循环
79 for(int i=0; i<G[v].size(); i++)
80 {
81 edge &e=G[v][i];
82 int d2=e.w+d;
83 if(dis[e.to]>dis[v]+e.w)//更新最短路径if(dis[e.to]>d2)
84 {
85 swap(dis[e.to],d2);
86 q.push(pii(dis[e.to],e.to));
87 }
88 if(dis2[e.to]>d2&&dis[e.to]<d2)//注意这个两个条件,后面那个条件可以不要
89 {
90 swap(dis2[e.to],d2);
91 q.push(pii(dis2[e.to],e.to));
92 }
93 }
94 }
95 Pri(dis2[n]);
96 }
97 int main()
98 {
99 Sci2(n,r);
100 For_(i,1,r)
101 {
102 int u,v,w;
103 Sci3(u,v,w);
104 G[u].push_back(edge(v,w));
105 G[v].push_back(edge(u,w));
106 }
107 solve();
108 return 0;
109 }
^__^好吧,我现在已经废到连一个次短路径都看不懂了,唉,这道题写于大二上学期的期末考试考完的这天晚上,浪了一个学期,感觉啥都没干,此时想到暑假结束时的目标。。。。。。。。君子立长志,小人常立志  真香!寒假快开始了,真的得改改了!

Roadblocks的更多相关文章

  1. poj 3255 Roadblocks

    Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13216 Accepted: 4660 Descripti ...

  2. POJ 3255 Roadblocks(A*求次短路)

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12167   Accepted: 4300 Descr ...

  3. Bzoj 1726: [Usaco2006 Nov]Roadblocks第二短路 dijkstra,堆,A*,次短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 969  Solved: 468[S ...

  4. BZOJ1726: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 768  Solved: 369[S ...

  5. BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路( 最短路 )

    从起点和终点各跑一次最短路 , 然后枚举每一条边 , 更新answer ---------------------------------------------------------------- ...

  6. BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她 ...

  7. 1726: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 835  Solved: 398[S ...

  8. poj3255 Roadblocks

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13594   Accepted: 4783 Descr ...

  9. P2865 [USACO06NOV]路障Roadblocks

    P2865 [USACO06NOV]路障Roadblocks 最短路(次短路) 直接在dijkstra中维护2个数组:d1(最短路),d2(次短路),然后跑一遍就行了. attention:数据有不同 ...

  10. poj 3255 Roadblocks 次短路(两次dijksta)

    Roadblocks Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

随机推荐

  1. SSH(三)创建包和第一个页面

    在ssh web项目src下一次创建 com.ssh/ .action .dao .entity .service 四个包,example: 在entity包下创建class,name: produc ...

  2. 4.4:Sqoop数据导入实验

    〇.概述 1.拓扑结构 2.目标 使用sqoop工具将数据从mysql数据库导入到HDFS和Hbase 一.配置免密登录hdfs 三.导入到hdfs中 sqoop import --connect j ...

  3. 4.2:Scrapy爬虫

    使用Scrapy框架对网站的内容进行爬取 在桌面处打开终端,并在终端中输入: scrapy startproject bitNews cd bitNews/bitNews 修改items文件的内容,输 ...

  4. 【Impala】概念、原理、内外部shell、建库建表、分区、查询、自定义函数、存储压缩

    一.基本概念 1.介绍 对HDFS.Hbase数据的高性能.低延迟的交互式SQL查询功能 2.优缺点 优点:基于内存运算,无需写入磁盘,无需转化为MR,支持Data Locality调度(数据和计算在 ...

  5. .net6+wpf制作指定局域网ip无法上网的arp欺诈工具

    摘一段来自网上的arp欺诈解释:ARP欺骗(ARP spoofing),又称ARP毒化(ARP poisoning,网络上多译为ARP病毒)或ARP攻击,是针对以太网地址解析协议(ARP)的一种攻击技 ...

  6. 使用pandas处理数据和matplotlib生成可视化图表

    一.缘由 上一篇输入关键词"口红",将淘宝中的的相关商品信息全部爬取了下拉,并且以CSV的文件格式储存.我们拿到数据之后,那么就需要对数据进行处理.只是将爬取到的数据以更直观的方式 ...

  7. java中的静态属性和静态方法

    本文主要讲述java的静态变量和静态方法 静态变量和静态方法,随着类加载完成,而完成,随着类的消失,而销毁. 静态方法只能调用静态变量/方法:普通方法,既能调用静态变量/方法,也能调用非静态变量/方法 ...

  8. 【深入浅出 Yarn 架构与实现】4-3 RM 管理 NodeManager

    本篇继续对 RM 中管理 NodeManager 的部分进行深入的讲解.主要有三个部分:检查 NM 是否存活:管理 NM 的黑白名单:响应 NM RPC 请求. 一.简介 在 RM 的主从结构中,最主 ...

  9. MongoDB从入门到实战之MongoDB工作常用操作命令

    前言: 上一章节我们快速的在Docker容器中安装了MongoDB,并且通过Navicat MongoDB可视化管理工具快速的连接.创建数据库.集合以及添加了文档数据源.这一章节我们主要是了解一下在日 ...

  10. [python]《Python编程快速上手:让繁琐工作自动化》学习笔记4

    1. 处理Excel 电子表格笔记(第12章)(代码下载) 本文主要介绍openpyxl 的2.5.12版处理excel电子表格,原书是2.1.4 版,OpenPyXL 团队会经常发布新版本.不过不用 ...