Roadblocks http://poj.org/problem?id=3255

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: 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)

dijkatra算法可以求最短路和次短路

 1 #include"iostream"
 2 #include"cstdio"
 3 #include"cstring"
 4 #include"algorithm"
 5 #include"cstdlib"
 6 #include"queue"
 7 #include"vector"
 8 using namespace std;
 9 const int inf=0x7fffffff;
10 const int ms=100001;
11 struct edge
12 {
13     //int from;
14     int to;
15     int cost;
16 };
17 typedef pair<int,int> P;
18 int main()
19 {
20     int i,j,N,R,ans,a,b,w;
21     scanf("%d%d",&N,&R);
22     vector<edge> vec[N+1];
23     for(i=1;i<=R;i++)
24     {
25         scanf("%d%d%d",&a,&b,&w);
26         edge e;
27         e.to=b;
28         e.cost=w;
29         //vec[a].push_back(edge{b,w});  有警告
30         vec[a].push_back(e);
31         e.to=a;
32         vec[b].push_back(e);
33     }
34     int dist[N+1],dist2[N+1];
35     priority_queue<P,vector<P>,greater<P> > que;
36     fill(dist,dist+N+1,inf);
37     fill(dist2,dist2+N+1,inf);
38     dist[1]=0;
39     que.push(P(0,1));
40     while(!que.empty())
41     {
42         P p=que.top();
43         que.pop();
44         int v=p.second;
45         int d=p.first;
46         if(dist2[v]<d)
47             continue;
48         for(i=0;i<vec[v].size();i++)
49         {
50             edge &e=vec[v][i];
51             int d2=d+e.cost;
52             if(dist[e.to]>d2)
53             {
54                 swap(dist[e.to],d2);
55                 que.push(P(dist[e.to],e.to));
56             }
57             if(dist2[e.to]>d2&&dist[e.to]<d2)
58             {
59                 dist2[e.to]=d2;
60                 que.push(P(dist2[e.to],e.to));
61             }
62         }
63     }
64     //printf("%d\n",dist2[N]);
65     cout<<dist2[N]<<endl;
66     return 0;
67 }

Roadblocks http://poj.org/problem?id=3255

时间: 2024-10-10 05:21:51

Roadblocks http://poj.org/problem?id=3255的相关文章

poj 1651 http://poj.org/problem?id=1651

http://poj.org/problem?id=1651Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6188   Accepted: 3777 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. Dur

http://poj.org/problem?id=1330

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17818   Accepted: 9455 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each

POJ3278http://poj.org/problem?id=3278

http://poj.org/problem?id=3278 题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步 #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #include<queue> #define max(a, b)(a > b ? a : b) #define N 100010 using namespace s

http://poj.org/problem?id=2112_网络流

1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 using namespace std; 7 8 #define MAX 300 9 #define INF 10000000 10 int dis[MAX][MAX]; // 任意两点间的最短路径 11 int map[MAX][MAX];

http://poj.org/problem?id=3278(bfs)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 76935   Accepted: 24323 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00

poj 1915 http://poj.org/problem?id=1915

/**< */#include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> #include <ctype.h> #define N 310 using namespace std; int d[8][2] = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, -1}, {2, 1}

[题解]poj.org Problem#3468

Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. In

poj 2769 Reduced ID Numbers(memset使用技巧)

Description T. Chur teaches various groups of students at university U. Every U-student has a unique Student Identification Number (SIN). A SIN s is an integer in the range 0 ≤ s ≤ MaxSIN with MaxSIN = 106-1. T. Chur finds this range of SINs too larg

POJ 2769 Reduced ID Numbers

思路: 枚举 Reduced ID Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8847 Accepted: 3552 Description T. Chur teaches various groups of students at university U. Every U-student has a unique Student Identification Number (SIN). A SIN s