lightoj 1099 - Not the Best(次短路)
2016-09-17 12:23
1076 查看
1099 - Not the Best
Robin has moved to a small village and sometimes enjoys returning to visit one of his best friends. He does not want to get to his old home too quickly, because he likes the scenery along the way. He has decided to take the second-shortest rather than the
shortest path. He knows there must be some second-shortest path.
The countryside consists of R bidirectional roads, each linking two of the N intersections, conveniently numbered from 1 to N. Robin starts at intersection 1, and his 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).
Each case contains two integers N (1 ≤ N ≤ 5000) and R (1 ≤ R ≤ 105). Each of the next R lines contains three space-separated integers: u, v and w that describe
a road that connects intersections u and v and has length w (1 ≤ w ≤ 5000).
/*
思路:
把求最短路时更新最短路的那部分改一下。
dis1,dis2数组分别记录到该点的最短路和次短路
分三种情况:
1.若该点最短路+下一条边比到下个点的最短路短,则更新下个点的最短路,同时更新次短路为原最短路
2.若该点次短路+下一条边比到下个点的次短路短,则更新下个点的次短路
3.若该点最短路+下一条边比到下个点的最短路长同时比下个点的次短路短,则更新下个点的次短路
*/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
const int INF = 0x3f3f3f3f;
int n, m, k, head[maxn], dis1[maxn], dis2[maxn];
bool book[maxn];
struct node
{
int v, w, next;
}edge[maxn];
void addEdge(int u, int v, int w)
{
edge[k].v = v;
edge[k].w = w;
edge[k].next = head[u];
head[u] = k++;
}
void spfa(int u)
{
memset(dis1, INF, sizeof(dis1));
memset(dis2, INF, sizeof(dis2));
memset(book, 0, sizeof(book));
queue<int> q;
q.push(u);
dis1[u] = 0;
book[u] = 1;
while(!q.empty())
{
u = q.front(); q.pop();
book[u] = 0;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;
int w = edge[i].w;
if(dis1[v] > dis1[u]+w)
{
dis2[v] = dis1[v];
dis1[v] = dis1[u]+w;
if(!book[v]) book[v] = 1, q.push(v);
}
if(dis2[v] > dis2[u]+w)
{
dis2[v] = dis2[u]+w;
if(!book[v]) book[v] = 1, q.push(v);
}
if(dis1[v] < dis1[u]+w && dis2[v] > dis1[u]+w)
{
dis2[v] = dis1[u]+w;
if(!book[v]) book[v] = 1, q.push(v);
}
}
}
}
int main(void)
{
int t, ca = 1;
cin >> t;
while(t--)
{
k = 0;
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &m);
while(m--)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
addEdge(u, v, w);
addEdge(v, u, w);
}
spfa(1);
printf("Case %d: %d\n", ca++, dis2
);
}
return 0;
}
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
shortest path. He knows there must be some second-shortest path.
The countryside consists of R bidirectional roads, each linking two of the N intersections, conveniently numbered from 1 to N. Robin starts at intersection 1, and his 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
Input starts with an integer T (≤ 10), denoting the number of test cases.Each case contains two integers N (1 ≤ N ≤ 5000) and R (1 ≤ R ≤ 105). Each of the next R lines contains three space-separated integers: u, v and w that describe
a road that connects intersections u and v and has length w (1 ≤ w ≤ 5000).
Output
For each case, print the case number and the second best shortest path as described above.Sample Input | Output for Sample Input |
2 3 3 1 2 100 2 3 200 1 3 50 4 4 1 2 100 2 4 200 2 3 250 3 4 100 | Case 1: 150 Case 2: 450 |
思路:
把求最短路时更新最短路的那部分改一下。
dis1,dis2数组分别记录到该点的最短路和次短路
分三种情况:
1.若该点最短路+下一条边比到下个点的最短路短,则更新下个点的最短路,同时更新次短路为原最短路
2.若该点次短路+下一条边比到下个点的次短路短,则更新下个点的次短路
3.若该点最短路+下一条边比到下个点的最短路长同时比下个点的次短路短,则更新下个点的次短路
*/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
const int INF = 0x3f3f3f3f;
int n, m, k, head[maxn], dis1[maxn], dis2[maxn];
bool book[maxn];
struct node
{
int v, w, next;
}edge[maxn];
void addEdge(int u, int v, int w)
{
edge[k].v = v;
edge[k].w = w;
edge[k].next = head[u];
head[u] = k++;
}
void spfa(int u)
{
memset(dis1, INF, sizeof(dis1));
memset(dis2, INF, sizeof(dis2));
memset(book, 0, sizeof(book));
queue<int> q;
q.push(u);
dis1[u] = 0;
book[u] = 1;
while(!q.empty())
{
u = q.front(); q.pop();
book[u] = 0;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;
int w = edge[i].w;
if(dis1[v] > dis1[u]+w)
{
dis2[v] = dis1[v];
dis1[v] = dis1[u]+w;
if(!book[v]) book[v] = 1, q.push(v);
}
if(dis2[v] > dis2[u]+w)
{
dis2[v] = dis2[u]+w;
if(!book[v]) book[v] = 1, q.push(v);
}
if(dis1[v] < dis1[u]+w && dis2[v] > dis1[u]+w)
{
dis2[v] = dis1[u]+w;
if(!book[v]) book[v] = 1, q.push(v);
}
}
}
}
int main(void)
{
int t, ca = 1;
cin >> t;
while(t--)
{
k = 0;
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &m);
while(m--)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
addEdge(u, v, w);
addEdge(v, u, w);
}
spfa(1);
printf("Case %d: %d\n", ca++, dis2
);
}
return 0;
}
相关文章推荐
- LightOJ - 1099 Not the Best(次短路)
- [第2短路变形]LightOJ 1099 - Not the Best
- LightOj 1099 Not the Best(次短路)
- lightoj 1099 - Not the Best (次短路)
- 【最短路】LightOJ 1099 - Not the Best 第二短路
- LightOJ 1099 - Not the Best(Dijkstra次短路)
- uva_10740_Not the best(第k短路)
- poj 2449Remmarguts' Date uvaoj 10740 not the best dijkstra或spfa或bellman-ford k短路 A*
- (intermediate) 最短路(第k短路) UVA 10740 - Not the Best
- uva 10740 - Not the Best(第k短路)
- UVA-10740 - Not the Best (K短路)
- uva 10740 Not the Best (最短路 A*算法)
- The best way to avoid disappointment is to not expect anything from anyone!
- HDU 2680 Choose the best route 最短路(Dijkstra算法)
- HDU 2680 Choose the best route 【最短路 反向建图 dijkstra & SPFA 】
- HDOJ Choose the best route (最短路倒着构图Dijkstra)
- HDU2680 - Choose the best route - 最短路(多起点集合)
- HDU 2680 Choose the best route 最短路
- Choose the best route (最短路)
- hdu2680 Choose the best route(spfa求最短路模板题)