find the longest of the shortest (hdu 1595 SPFA+枚举)
find the longest of the shortest
Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1916 Accepted Submission(s): 668
Mirko overheard in the car that one of the roads is under repairs, and that it is blocked, but didn't konw exactly which road. It is possible to come from Marica's city to Mirko's no matter which road is closed.
Marica will travel only by non-blocked roads, and she will travel by shortest route. Mirko wants to know how long will it take for her to get to his city in the worst case, so that he could make sure that his girlfriend is out of town for long enough.Write
a program that helps Mirko in finding out what is the longest time in minutes it could take for Marica to come by shortest route by non-blocked roads to his city.
and Marica in city N.
In the next M lines are three numbers A, B and V, separated by commas. 1 ≤ A,B ≤ N, 1 ≤ V ≤ 1000.Those numbers mean that there is a two-way road between cities A and B, and that it is crossable in V minutes.
5 6
1 2 4
1 3 3
2 3 1
2 4 4
2 5 7
4 5 1 6 7
1 2 1
2 3 4
3 4 4
4 6 4
1 5 5
2 5 2
5 6 5 5 7
1 2 8
1 4 10
2 3 9
2 4 10
2 5 1
3 4 7
3 5 10
11
13
27
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1010
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std; struct Edge
{
int v,w;
int next;
}edge[maxn*maxn]; int head[maxn],inq[maxn],inqtime[maxn],dist[maxn];
int path[maxn];
int N,num,m,a[maxn],ok;
bool flag[maxn]; void addedge(int u,int v,int w)
{
edge[num].v=v;
edge[num].w=w;
edge[num].next=head[u];
head[u]=num++;
edge[num].v=u;
edge[num].w=w;
edge[num].next=head[v];
head[v]=num++;
} int SPFA(int s,int n)
{
int i;
memset(inq,0,sizeof(inq));
memset(dist,INF,sizeof(dist));
if (ok)
{
for (int i=0;i<=n;i++)
path[i]=-1;
}
path[s]=0;
queue<int>Q;
inq[s]=1;
dist[s]=0;
Q.push(s);
while (!Q.empty())
{
int u=Q.front();
Q.pop();
inq[u]=0;
for (i=head[u];i;i=edge[i].next)
{
if (dist[ edge[i].v ]>dist[u]+edge[i].w)
{
if (ok)
path[ edge[i].v ]=u;
dist[ edge[i].v ]=dist[u]+edge[i].w;
if (!inq[ edge[i].v ])
{
inq[ edge[i].v ]=1;
Q.push(edge[i].v);
}
}
}
}
return dist[N];
} int main()
{
int i,j;
while (~scanf("%d%d",&N,&m))
{
int u,v,w;
memset(head,0,sizeof(head));
num=1;
ok=1;
for (i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
}
// printf("num=%d\n",num);
SPFA(1,N);
ok=0;
j=N;
int ans=-1;
while (path[j]>0)
{
int x,y,value;
for (int i=head[j];i!=0;i=edge[i].next)
{
if (edge[i].v==path[j])
{
x=i;
value=edge[i].w;
edge[i].w=INF;
break;
}
}
for (int i=head[path[j]];i!=0;i=edge[i].next)
{
if (edge[i].v==j)
{
y=i;
edge[i].w=INF;
break;
}
}
ans=max(ans,SPFA(1,N));
edge[x].w=value;
edge[y].w=value;
// printf("j=%d\n",j);
j=path[j];
}
// printf("\n%d\n",path[1]);
printf("%d\n",ans);
}
return 0;
}
find the longest of the shortest (hdu 1595 SPFA+枚举)的更多相关文章
- hdu 1595 find the longest of the shortest【最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条】
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- hdu 1595 find the longest of the shortest(迪杰斯特拉,减去一条边,求最大最短路)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- hdu1595 find the longest of the shortest(Dijkstra)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1595 find the longest of the shortest Time Limit: 100 ...
- hdu 1595(最短路变形好题)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- hdu 1595 find the longest of the shortest(dijkstra)
Problem Description Marica is very angry with Mirko because he found a new girlfriend and she seeks ...
- hdu 1595 find the longest of the shortest
http://acm.hdu.edu.cn/showproblem.php?pid=1595 这道题我用spfa在枚举删除边的时候求最短路超时,改用dijkstra就过了. #include < ...
- hdu 1595 find the longest of the shortest(dijstra + 枚举)
http://acm.hdu.edu.cn/showproblem.php?pid=1595 大致题意: 给一个图.让输出从中删除随意一条边后所得最短路径中最长的. . 思路: 直接枚举每条边想必是不 ...
- HDU 1595 find the longest of the shortest【次短路】
转载请注明出处:http://blog.csdn.net/a1dark 分析:经典的次短路问题.dijkstra或者SPFA都能做.先找出最短路.然后依次删掉没条边.为何正确就不证明了.了解思想直接A ...
- HDU 5636 Shortest Path(Floyed,枚举)
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tot ...
随机推荐
- 解决img标签上下出现间隙的方法
图片与父元素下边缘有 2px 的间隙,并不是因为空格.多个 inline-block 元素之间的间隙才是因为空格. 任何不是块级元素的可见元素都是内联元素,其表现的特性是“行布局”形式.----< ...
- appium+python自动化-xpath定位
基本属性定位 以淘宝app为例,定位左上角扫一扫按钮 1.可以通过text文本定位到 //*[@text='text文本属性'] # 定位text driver.find_element_by_xpa ...
- Appium+python自动化-环境搭建
一.前言 本文是在windows10 X64系统下搭建appium,流程如下: 1.安装jdk1.8+python3.6 (64位) 2.安装node.js 3.安装Appium-desktop 4 ...
- jsp jstl标签库 el表达式
一.JSTL标签是什么? 提供了对国际化(I18N)的支持,它可以根据发出请求的客户端地域的不同来显示不同的语言. 同时还提供了格式化数据和日期的方法.实现这些功能需要I18N格式标签库(I18N-c ...
- XML文件中<return_code><![CDATA[SUCCESS]]></return_code>中CDATA的用法
转义字符不合法的XML字符必须被替换为相应的实体. 如果在XML文档中使用类似"<" 的字符, 那么解析器将会出现错误,因为解析器会认为这是一个新元素的开始.所以不应该象下面 ...
- BZOJ 1197: [HNOI2006]花仙子的魔法【DP】
Description 相传,在天地初成的远古时代,世界上只有一种叫做“元”的花.接下来,出 现了一位拥有魔法的花仙子,她能给花附加属性,从此,“元”便不断变异,产生了大千世界千奇百怪的各种各样的花. ...
- Snmp的学习总结(一)
摘自:http://blog.csdn.net/shanzhizi/article/details/11606767 目录(?)[-] SNMP的5种协议数据单元 SNMP的运行过程 11 Get ...
- 【bzoj1193】[HNOI2006]马步距离
[HNOI2006]马步距离 Description Input 只包含4个整数,它们彼此用空格隔开,分别为xp,yp,xs,ys.并且它们的都小于10000000. Output 含一个整数,表示从 ...
- __new__ 与 __init__的区别
原文地址:http://www.cnblogs.com/ifantastic/p/3175735.html 单例模式 class Singleton(object): def __new__(cls, ...
- [NOIP2012T3]开车旅行
题目描述 NOIP 2012 提高组 题3小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 ...