Harry Potter and the Final Battle
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4268 Accepted Submission(s): 1290
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.
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
2011 Multi-University Training Contest 15 - Host by WHU
输出删除一条边后的最短路的最大值,不能到达输出-1
只有删除1到n最短路上的边才影响最短路的长度,所以枚举最短路上的边就可以了,每删除一条边就求一次最短路,记录最大值,最短路上的边共n-1条,用dij+堆优化时间复杂度为n*n*logm
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 1005;
const int MAXM = 100005;
int tot,ans,head[MAXN];
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
struct Edge
{
int from,to,Next,w;
}edge[MAXM];
struct node
{
int u,dis;
node(){}
node(int _u,int _dis)
{
u = _u;
dis = _dis;
}
bool operator <(const struct node& a) const
{
return dis > a.dis;
}
};
void init()
{
tot = 0;
ans = 0;
memset(head,-1,sizeof(head));
memset(pre,0,sizeof(pre));
}
void addedge(int u,int v,int w)
{
edge[tot].from = u;
edge[tot].to = v;
edge[tot].w = w;
edge[tot].Next = head[u];
head[u] = tot++;
}
void Dijkstra(int s,int id)
{
int u,v;
struct node t;
priority_queue<node> pq;
memset(dis,INF,sizeof(dis));
memset(vis,false,sizeof(vis));
dis[s] = 0;
pq.push(node(s,dis[s]));
while(!pq.empty()) {
t = pq.top();
pq.pop();
u = t.u;
if(vis[u]) continue;
vis[u] = true;
for(int i = head[u]; i != -1; i = edge[i].Next) {
if(i == id || (i ^ 1) == id) continue;
v = edge[i].to;
if(dis[v] > dis[u] + edge[i].w) {
dis[v] = dis[u] + edge[i].w;
if(id == -1) {
pre[v] = i;
}
pq.push(node(v,dis[v]));
}
}
}
}
vector<int> vc;
int main(void)
{
int T,n,m,u,v,w;
scanf("%d",&T);
while(T--) {
init();
vc.clear();
scanf("%d %d",&n,&m);
while(m--) {
scanf("%d %d %d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
Dijkstra(1,-1);
if(dis[n] == -1) {
printf("-1\n");
continue;
}
int cur = n;
while(1) {
if(cur == 1) break;
vc.push_back(pre[cur]);
cur = edge[pre[cur]].from;
}
for(int i = 0; i < vc.size(); i++) {
Dijkstra(1,vc[i]);
ans = max(ans,dis[n]);
}
if(ans == INF) printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}