1.题目描述:
Harry Potter and the Final Battle
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3535 Accepted Submission(s): 1009
Problem Description
The final battle is coming. Now Harry Potter is located at city 1, and Voldemort is located at city n. To make the world peace as soon as possible, Of course, Harry Potter will choose the shortest road between city 1 and city n. But unfortunately, Voldemort is so powerful that he can choose to destroy any one of the existing roads as he wish, but he can only destroy one. Now given the roads between cities, you are to give the shortest time that Harry Potter can reach city n and begin the battle in the worst case.
Input
First line, case number t (t<=20).
Then for each case: an integer n (2<=n<=1000) means the number of city in the magical world, the cities are numbered from 1 to n. Then an integer m means the roads in the magical world, m (0< m <=50000). Following m lines, each line with three integer u, v, w (u != v,1 <=u, v<=n, 1<=w <1000), separated by a single space. It means there is a bidirectional road between u and v with the cost of time w. There may be multiple roads between two cities.
Then for each case: an integer n (2<=n<=1000) means the number of city in the magical world, the cities are numbered from 1 to n. Then an integer m means the roads in the magical world, m (0< m <=50000). Following m lines, each line with three integer u, v, w (u != v,1 <=u, v<=n, 1<=w <1000), separated by a single space. It means there is a bidirectional road between u and v with the cost of time w. There may be multiple roads between two cities.
Output
Each case per line: the shortest time to reach city n in the worst case. If it is impossible to reach city n in the worst case, output “-1”.
Sample Input
3 4 4 1 2 5 2 4 10 1 3 3 3 4 8 3 2 1 2 5 2 3 10 2 2 1 2 1 1 2 2
Sample Output
15 -1 2
Author
tender@WHU
Source
Recommend
lcy
给你一个无向图,然后找出当中的最短路,除去最短路中的随意一条边,看最糟糕的情况下,新的图中,第一个点到末点的最短路长度是多少。
3.解题思路:题目问的是最坏情况下,也是用路径记录未拆边前的最短路径,在路径上拆,再记录最坏情况,如果是INF则直接是-1
4.AC代码:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define N 1111
#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 u, v, w, next;
} E[maxn];
bool vis[N];
int head[N], dis[N], pa[N];
int cnt;
void init()
{
memset(head, -1, sizeof(head));
cnt = 0;
}
void addedge(int u, int v, int w)
{
E[cnt].u = u;
E[cnt].v = v;
E[cnt].w = w;
E[cnt].next = head[u];
head[u] = cnt++;
}
void spfa(int sta, int n, int key)
{
memset(vis, 0, sizeof(vis));
fill(dis, dis + n + 1, INF);
deque<int> q;
vis[sta] = 1;
dis[sta] = 0;
q.push_back(sta);
if (key == -1)
memset(pa, -1, sizeof(pa));
while (!q.empty())
{
int u = q.front();
q.pop_front();
vis[u] = 0;
for (int i = head[u]; i != -1; i = E[i].next)
{
if (i == key)
continue;
int v = E[i].v;
int w = E[i].w;
if (dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
if (key == -1)
pa[v] = i;
if (!vis[v])
{
vis[v] = 1;
if (!q.empty() && dis[v] <= dis[q.front()])
q.push_front(v);
else
q.push_back(v);
}
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int t, m, n;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
init();
while (m--)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
addedge(v, u, w);
}
spfa(1, n, -1);
int ans = dis[n];
for (int i = pa[n]; i != -1 && ans != INF; i = pa[E[i].u])
{
spfa(1, n, i);
ans = max(ans, dis[n]);
}
printf("%d\n", ans == INF ? -1 : ans);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}