Harry Potter and the Final Battle

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
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std; const int N=1010;
const int M=100010;
const int INF=0xffffff; struct Edge
{
int u;
int to;
int w;
int flag;
int next;
} e[M]; int head[N];
int dist[N];
int path[N];
int inq[N];
int n,m,cnt,flag; void AddEdge(int u,int v,int w)
{
e[cnt].u=u;
e[cnt].to=v;
e[cnt].w=w;
e[cnt].flag=1;
e[cnt].next=head[u];
head[u]=cnt++;
} int SPFA(int s)
{
queue<int>Q;
for(int i=1; i<=n; i++)
{
dist[i]=INF;
inq[i]=0;
}
dist[s]=0;
inq[s]=1;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
inq[u]=0;
for(int j=head[u]; j!=-1; j=e[j].next)
{
int x=e[j].to;
if(e[j].flag&&dist[x]>dist[u]+e[j].w)
{
dist[x]=dist[u]+e[j].w;
if(!flag)
path[x]=j;
if(!inq[x])
{
Q.push(x);
inq[x]=1;
}
}
}
}
return dist[n];
} int main()
{
//freopen("C:\\Users\\Administrator\\Desktop\\kd.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
cnt=flag=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);
}
memset(path,-1,sizeof(path));
SPFA(1);
flag=1;
int i=n,j=-1;
int res=-1;
while(path[i]!=-1)
{
j=path[i];
e[j].flag=e[j+1].flag=0;
int tmp=SPFA(1);
e[j].flag=e[j+1].flag=1;
if(tmp>res)
res=tmp;
i=e[j].u;
}
if(res<INF)
printf("%d\n",res);
else
puts("-1");
}
}

枚举最短路径+SPFA的更多相关文章

  1. [最短路径SPFA] POJ 1847 Tram

    Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...

  2. 最短路径--SPFA 算法

    适用范围:给定的图存在负权边,这时类似Dijkstra等算法便没有了用武之地,而Bellman-Ford算法的复杂度又过高,SPFA算法便派上用场了. 我们约定有向加权图G不存在负权回路,即最短路径一 ...

  3. 最短路径 SPFA P3371 【模板】单源最短路径(弱化版)

    P3371 [模板]单源最短路径(弱化版) SPFA算法: SPFA 算法是 Bellman-Ford算法 的队列优化算法的别称,通常用于求含负权边的单源最短路径,以及判负权环.SPFA 最坏情况下复 ...

  4. 最短路径——SPFA算法

    一.前提引入 我们学过了Bellman-Ford算法,现在又要提出这个SPFA算法,为什么呢? 考虑一个随机图(点和边随机生成),除了已确定最短路的顶点与尚未确定最短路的顶点之间的边,其它的边所做的都 ...

  5. 图的最短路径-----------SPFA算法详解(TjuOj2831_Wormholes)

    这次整理了一下SPFA算法,首先相比Dijkstra算法,SPFA可以处理带有负权变的图.(个人认为原因是SPFA在进行松弛操作时可以对某一条边重复进行松弛,如果存在负权边,在多次松弛某边时可以更新该 ...

  6. luogu P3371 & P4779 单源最短路径spfa & 最大堆优化Dijkstra算法

    P3371 [模板]单源最短路径(弱化版) 题目背景 本题测试数据为随机数据,在考试中可能会出现构造数据让SPFA不通过,如有需要请移步 P4779. 题目描述 如题,给出一个有向图,请输出从某一点出 ...

  7. 最短路径----SPFA算法

    求最短路径的算法有许多种,除了排序外,恐怕是ACM界中解决同一类问题算法最多的了.最熟悉的无疑是Dijkstra,接着是Bellman-Ford,它们都可以求出由一个源点向其他各点的最短路径:如果我们 ...

  8. LD1-B(最短路径-SPFA)

    题目链接 /* *题目大意: *给定v个点的重量,并给定e条边,每条边具有一个权值; *在e条边中选v-1条边使这v个点成为一棵树; *定义这棵树的代价为(每棵子树节点重量和其子树根到父节点的边的权值 ...

  9. 【SPFA与Dijkstra的对比】CDOJ 1961 咸鱼睡觉觉【差分约束-负权最短路径SPFA】

    差分约束系统,求最小值,跑最长路. 转自:https://www.cnblogs.com/ehanla/p/9134012.html 题解:设sum[x]为前x个咕咕中至少需要赶走的咕咕数,则sum[ ...

随机推荐

  1. JSP——九大内置对象和其四大作用域

    一.JSP九大内置对象: JSP根据Servlet API 规范提供了某些内置对象,开发者不用事先声明就可以使用标准的变量来访问这些对象. Request:代表的是来自客户端的请求,例如我们在FORM ...

  2. 在InteliJ IDEA中写Dart及配置IDEA - Dart Plugin

    此文运用的是优雅的Markdown而书 Dart官方建议使用的编译器是DartEditor,我下载下来看下,和Eclipse的界面很相像.对于Eclipse,我是既爱又恨,爱它的稳定,恨它的功能没有I ...

  3. xar安装使用方法

    xar是一种扩展的归档格式(eXtensible ARchive format),是一种开源的文件格式.xar文件在Mac OS X 10.5里是用于软件安装程序. ---------------- ...

  4. 全新安装mysql最新版本

    写在前面: 下面写的东西只是最近安装的一个说明,是在系统中没存在mysql的情况下安装的,后期会根据官方文档写一个详细有价值的文档 安装原理:利用mysql官方的mysql_apt-repositor ...

  5. JDK JRE先保存 后面再整理

    1. 定义 JRE(Java Runtime Enviroment)是Java的运行环境.面向Java程序的使用者,而不是开发者.如果你仅下载并安装了JRE,那么你的系统只能运行Java程 序.JRE ...

  6. 转:基于node的web开发框架Express入门

    JavaScript 标准参考教程(alpha) 草稿二:Node.js Express框架 GitHub TOP Express框架 来自<JavaScript 标准参考教程(alpha)&g ...

  7. 基于Visual C++2013拆解世界五百强面试题--题11-查找数字出现次数

    在排序数组中,找出给定数字出现的次数比如{ 1, 2, 2, 2, 3}中2的出现次数是3次 我们可使用二分查找发,分别查找出2最先出现的位置和最后出现的位置相减即可. 下面是上代码: #includ ...

  8. android 构建数据库SQLite

    1.首先我们需要一个空白的eclipse android工程 2.然后修改AndroidManifest.xml 在<application></application>标签里 ...

  9. hdoj 3018 Ant Trip(无向图欧拉路||一笔画+并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 思路分析:题目可以看做一笔画问题,求最少画多少笔可以把所有的边画一次并且只画一次: 首先可以求出 ...

  10. Ext JS学习第五天 Ext_window组件(二)

    此文用来记录学习笔记 •上一讲我们已经学过了window的使用,那么在这将中,我们将结合然后把Ext中需要注意的地方,以及组建的使用给予介绍.indow做几个Web开发的经典示例. •ExtWeb实战 ...