hdu 3986(最短路变形好题)
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
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.
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.
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”.
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
-1
2
#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(最短路变形好题)的更多相关文章
- hdu 1595(最短路变形好题)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- HDU 2544 最短路(模板题——Floyd算法)
题目: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你 ...
- HDU 2544最短路dijkstra模板题
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- HDU 1596 最短路变形
这道题怎么都是TLE,报警了,先放在这 http://acm.hdu.edu.cn/showproblem.php?pid=1596 #include <iostream> #includ ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 2544 最短路 【Dijkstra模板题】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路:最短路的模板题 Dijkstra 算法是一种类似于贪心的算法,步骤如下: 1.当到一个点时, ...
- 最短路变形题目 HDU多校7
Mr.Quin love fishes so much and Mr.Quin’s city has a nautical system,consisiting of N ports and M sh ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- POJ-2253.Frogger.(求每条路径中最大值的最小值,最短路变形)
做到了这个题,感觉网上的博客是真的水,只有kuangbin大神一句话就点醒了我,所以我写这篇博客是为了让最短路的入门者尽快脱坑...... 本题思路:本题是最短路的变形,要求出最短路中的最大跳跃距离, ...
随机推荐
- <Docker学习>1. 简介
Q: Dokcer是什么? A: 是一种虚拟化技术.参考https://www.imooc.com/learn/867快速了解Docker. Q: 传统虚拟机技术和Dokcer的区别? A: 传统虚拟 ...
- 权限组件(15):rbac的使用文档和在业务中的应用
这里用主机管理系统当做示例. 一.将rbac组件拷贝到项目中. 注意: rbac自己的静态文件.layout.html(被继承的模板).bootstrap.fontsize.公共的css.jquery ...
- IQueryable与IEnumerable区别
前者可以延迟加载,即执行完后不马上执行数据库语句,用到再加载.
- (PowerDesigner&Sqlite)PD中设计完表后,将其导入数据库中
本人连接过SQLServer跟SQLite Ⅰ.SQLServer,百度,转一下:http://jingyan.baidu.com/article/7f766daf465e9c4101e1d0d5.h ...
- Java模拟音乐播放器 暂停与重新播放——线程如何控制另外一个线程的状态
package com.example.Thread; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEve ...
- CSU 1326: The contest(分组背包)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1326 题意: n个题目,每个题目都有一个价值Pi和相对能力消耗Wi,但是有些题目因为太坑不能同时做 ...
- Android使用Glide加载Gif.解决Glide加载Gif非常慢问题
在Glide文档中找了半天没发现加载Gif的方式.然后通过基本的用法去加载: Glide.with(MainActivity.this).load(url).asGif().into(imageVie ...
- HashMap 简介
HashMap 前置条件 了解数组 了解链表 jdk version: 1.8 个人分3步来了解HashMap 通过数据结构图 通过为了完成这样的数据结构我们该怎么做 HashMap 实际put方法源 ...
- loj2052 「HNOI2016」矿区
学习一发平面图的姿势--ref #include <algorithm> #include <iostream> #include <cstdio> #includ ...
- mac攻略(八) -- 神器zsh和iterm2的配置
1. 安装oh my zsh 安装命令: curl -L http://install.ohmyz.sh | sh 修改shell的方式: chsh -s /bin/zsh 2.安装cask( ...