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. Assignment HDU - 2853(二分图匹配 KM 新边旧边)

    传送门: Assignment HDU - 2853 题意:题意直接那松神的题意了.给了你n个公司和m个任务,然后给你了每个公司处理每个任务的效率.然后他已经给你了每个公司的分配方案,让你求出最多能增 ...

  2. A1095 Cars on Campus (30)(30 分)

    A1095 Cars on Campus (30)(30 分) Zhejiang University has 6 campuses and a lot of gates. From each gat ...

  3. LightOJ 1141 Number Transformation

    Number Transformation In this problem, you are given an integer number s. You can transform any inte ...

  4. Nodejs-模块化结构

    1.模块(一个文件就是一个模块) 获取当前脚本所在的路径 _ _dirname 文件路径 _ _filename (1)创建模块(module1.js) const fs=require('fs'); ...

  5. Windows网络编程笔记6 --- WinSock I/O 控制方法

    Windows提供了两种方式“套接字模式”和“套接字I/O模型”,可对一个套接字上的I/O行为加以控制.套接字模式用于决定在随一个套接字调用时,那些 Winsock函数的行为.其中的模型包括括sele ...

  6. Pipenv 学习笔记

    个人笔记,胡言乱语.并不是什么教学向文章.. 前言 在学习了 Python.Java 后,会发现 Java 有很成熟的项目构建工具,以前是使用 xml 的 Maven,现在又出现了使用 groovy ...

  7. Spring MVC与jQuery结合使用Ajax技术

    gradle配置 group 'org.zln.webDemo' version '1.0-SNAPSHOT' apply plugin: 'java' apply plugin: 'jetty' s ...

  8. 省选算法学习-插头dp

    插头dp?你说的是这个吗? 好吧显然不是...... 所谓插头dp,实际上是“基于连通性的状态压缩dp”的简称,最先出现在cdq的论文里面 本篇博客致力于通过几道小小的例题(大部分都比较浅显)来介绍一 ...

  9. 出租车(taxi)

    出租车(taxi) 题目描述 Bessie在农场上为其他奶牛提供出租车服务.这些奶牛已经在沿着长度为M(1<= M <= 1,000,000,000)的栅栏上不同的地点聚集等候.不幸的是, ...

  10. wordpress install 主题

    手动安装,你可到访WordPress官方网站的主题部分,找到你喜欢的主题后,可压缩下载到电脑并将其解压缩: 上传.zip 包到服务器,解压到 wordpress/wp-content/themes c ...