1.题目描述:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14158 | Accepted: 4976 |
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
Sample Input
4 4 1 2 100 2 4 200 2 3 250 3 4 100
Sample Output
450
Hint
Source
2.题意概述:给你一个图,小明比较爱玩,想多欣赏路上风景,因此选择次短路出发,问你图的次短路长度
3.解题思路:
暴力成就一切,分别对起点和终点做一遍最短路(记录起点最短路为dis1,终点的最短路为dis2),然后枚举每一条边,把u到v改写成dis1[u] + dis2[v] + distance_between_u_v;
因为最短路可能有多条,因此最好用个set维护一下,或者在最短路基础上另外更新。
4.AC代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <ctime>
#define INF 0x3f3f3f3f
#define maxn 100100
#define N 5111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
struct node
{
int v, w;
node(int a, int b) { v = a; w = b; }
friend bool operator< (node a, node b)
{
return a.w > b.w;
}
};
vector<node> mp[N];
set<int> s;
int dis1[N], dis2[N];
bool vis[N];
void spfa(int sta, int n, int dis[])
{
memset(vis, 0, sizeof(vis));
fill(dis, dis + n + 1, INF);
priority_queue<node> q;
vis[sta] = 1;
dis[sta] = 0;
q.push(node(sta, 0));
while (!q.empty())
{
int u = q.top().v;
q.pop();
vis[u] = 0;
int sz = mp[u].size();
for (int i = 0; i < sz; i++)
{
int v = mp[u][i].v;
int val = mp[u][i].w;
if (dis[v] > dis[u] + val)
{
dis[v] = dis[u] + val;
if (!vis[v])
{
vis[v] = 1;
q.push(node(v, dis[v]));
}
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n, r;
while (~scanf("%d%d", &n, &r))
{
for (int i = 1; i <= n; i++)
mp[i].clear();
for (int i = 0; i < r; i++)
{
int a, b, d;
scanf("%d%d%d", &a, &b, &d);
mp[a].push_back(node(b, d));
mp[b].push_back(node(a, d));
}
spfa(1, n, dis1);
spfa(n, n, dis2);
s.clear();
for (int i = 1; i <= n; i++)
{
int sz = mp[i].size();
for (int j = 0; j < sz; j++)
{
int to = mp[i][j].v;
int val = mp[i][j].w;
s.insert(dis1[i] + dis2[to] + val);
}
}
set<int>::iterator it = s.begin();
printf("%d\n", *(++it));
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}