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. Bank Simulation Program银行管理系统C++ :)

    设计并实现简单的银行存取款系统,系统主界面包括登录和注册两个选项,选择登录,提示用户输入银行帐号和密码,验证通过后进入主界面,主界面包括:存款.取款.查询余额.历史记录.修改密码等功能.注册功能让用户 ...

  2. Python知识点入门笔记——基本运算和表达式

    变量:Python的变量不需要单独定义,直接在赋值的过程中完成定义. 当直接运行一个没有赋值过的变量时,会报错. 当不需要某个变量时,可以用del来删除 每个变量都占据着一定的内存空间,当变量被删除了 ...

  3. [Poj2761]Feed the dogs(主席树)

    Desciption 题意:求区间第K小(N<=100000) Solution 主席树模板题 Code #include <cstdio> #include <algorit ...

  4. 2、python中的数字

    第二篇开始谈谈python中的数据. 一.前言 python中的数字包含了整数.浮点数.复数三种.在python的早期版本,或许可以看到正数被分为长整数与短整数,后来被取消了,因此这里不作讨论.通常我 ...

  5. unity安装记录

    官网直接下载(http://unity3d.com/cn/) 下载完后,会要求输入账号密码.我是先在官网进行一个账号注册,然后登陆即可. 首先会要你输入一个已有的邮箱,进行认证.进入自己邮箱,点击链接 ...

  6. Java文件 ---流

    分类 根据数据走向,分为输入流.输出流 根据处理的数据类型,分为字节流.字符流 字节流 可以处理所有类型的数据,如MP3.图片.文字.视频等.在读取时,读到一个字节就返回一个字节. 在Java中对应的 ...

  7. MySQL基础4-SQL简单查询(单表)

    1.SELECT语句 2.运算符的优先级 利用Navicat中的查询方法: 栗子1:查询所有货品信息 栗子2:查询所有货品的id,productName,salePrice 当查询错误的时候出现的界面 ...

  8. C++模板编程-模板基础重点

    模板基础 1.模板参数自动推导,如果是已知的参数类型与个数,这调用模板时可以不写类型. Cout<<max<int>(1,3);可以写为Cout<<max(1,3) ...

  9. 使用 SceneLoader 类在 XNA 中显示载入屏幕(十)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  10. 使用charles进行https抓包

    一.charles电脑端设置 1.在Charles的菜单栏上选择"Proxy"->"Proxy Settings",填入代理端口8888(这个端口不一定填 ...