Harry Potter and the Final Battle

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3239    Accepted Submission(s): 906

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
1595不需要考虑重边,而这道题要考虑,所以要用链式前向星保存,比较好的解法是spfa,我们第一次求最短路的时候用一个数组保存下这条边,然后后面枚举最短路上面的边的时候需要用到这个数组,通过这个题又学到一个好方法,如何在遍历图保的时候存访问过的边。
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <queue>
using namespace std;
const int N = ;
const int M = ;
const int INF = ;
struct Edge
{
int u,v,w,next;
} edge[M];
int head[N];
int pre[N];
int n,m;
bool vis[N];
int low[N];
int _edge[M]; ///标记下哪条边用过了
void addEdge(int u,int v,int w,int &k)
{
edge[k].v = v,edge[k].w = w;
edge[k].next=head[u],head[u]=k++;
}
int spfa(int s,int flag)
{
queue<int> q;
for(int i=; i<=n; i++)
{
if(flag)
{
pre[i] = s;
}
low[i] = INF;
vis[i] = false;
}
low[s] = ;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int k = head[u]; k!=-; k=edge[k].next)
{
int v = edge[k].v,w=edge[k].w;
if(low[v]>low[u]+w)
{
low[v] = low[u]+w;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
if(flag)
{
pre[v] = u;
_edge[v] = k; ///标记最短路上哪条边被用了
}
}
}
}
if(low[n]>=INF) return -;
return low[n];
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
memset(head,-,sizeof(head));
scanf("%d%d",&n,&m);
int tot = ;
for(int i=; i<m; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addEdge(u,v,w,tot);
addEdge(v,u,w,tot);
}
int res = spfa(,);
if(res==-)
{
printf("-1\n");
continue;
}
int temp = n;
int Max= -;
while(temp!=)
{
int e = _edge[temp];
int k = edge[e].w;
edge[e].w=INF;
int res = spfa(,);
if(res==-)
{
Max = -;
break;
}
Max = max(Max,res);
edge[e].w=k;
temp = pre[temp];
}
printf("%d\n",Max);
}
}

hdu 3986(最短路变形好题)的更多相关文章

  1. hdu 1595(最短路变形好题)

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  2. HDU 2544 最短路(模板题——Floyd算法)

    题目: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你 ...

  3. HDU 2544最短路dijkstra模板题

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  4. HDU 1596 最短路变形

    这道题怎么都是TLE,报警了,先放在这 http://acm.hdu.edu.cn/showproblem.php?pid=1596 #include <iostream> #includ ...

  5. UESTC 30 &&HDU 2544最短路【Floyd求解裸题】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. HDU 2544 最短路 【Dijkstra模板题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路:最短路的模板题 Dijkstra 算法是一种类似于贪心的算法,步骤如下: 1.当到一个点时, ...

  7. 最短路变形题目 HDU多校7

    Mr.Quin love fishes so much and Mr.Quin’s city has a nautical system,consisiting of N ports and M sh ...

  8. ACM: HDU 2544 最短路-Dijkstra算法

    HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  9. POJ-2253.Frogger.(求每条路径中最大值的最小值,最短路变形)

    做到了这个题,感觉网上的博客是真的水,只有kuangbin大神一句话就点醒了我,所以我写这篇博客是为了让最短路的入门者尽快脱坑...... 本题思路:本题是最短路的变形,要求出最短路中的最大跳跃距离, ...

随机推荐

  1. python之格式化

    python有两种方式可以格式化一种是用%s,一种使用format(2.6)进入的,从下面的代码可以看出,效果差不多. name = 'edward' age = 27 print("My ...

  2. V4L2学习(二)结构介绍

    v4l2_device v4l2_device在v4l2框架中充当所有v4l2_subdev的父设备,管理着注册在其下的子设备.以下是v4l2_device结构体原型(去掉了无关的成员): struc ...

  3. Git-起步

    Git命令行 只要输入git,Git就会不带任何参数地列出它的选项和最常用的子命令. 要得到一个完整的git子命令列表,可以输入git help --all 显示版本号 git --version 每 ...

  4. 在MAC下使用Robotframework+Selenium2【第一枪】robotframework安装步骤

    最近使用苹果的MAC Pro本本,感受着苹果系统的新鲜,确实让我手忙脚乱一阵,毕竟使用windows系统太长时间了,刚开始用MAC Pro确实感觉别扭,用了一段,发现MAC系统还不错,好了,转入正题. ...

  5. time模块和datetime模块详解

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  6. 3 View - 状态保持 session

    1.状态保持 http协议是无状态的:每次请求都是一次新的请求,不会记得之前通信的状态 客户端与服务器端的一次通信,就是一次会话 实现状态保持的方式:在客户端或服务器端存储与会话有关的数据 存储方式包 ...

  7. django的as_view方法实现分析

    django的类视图拥有自动查找指定方法的功能, 通过调用是通过as_view()方法实现 urls.py from meduo_mall.demo import views urlpatterns ...

  8. day 16 JS DOM 继续

    为什么有jquey了还学DOM  ? 因为JQuey 是大而全,可能有10k 但是我们用到的只有1k  网站小没事,网站大流量就是问题 所以大网站都是自己用DOM 实现一个类似于JQuey 的适合自己 ...

  9. mybatis sql转义符号

    第一种写法:通过<![CDATA[ ]]>符号来写 大于等于:<![CDATA[ >= ]]> 小于等于:<![CDATA[ <= ]]> 例如:sql ...

  10. android实现前置后置摄像头相互切换

    首先自定义一个继承自SurfaceView并且实现了SurfaceHolder.Callback接口的组件: public class CameraView extends SurfaceView i ...