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 ...
随机推荐
- python基础学习笔记——开发规范
> 编码 1 2 3 4 5 所有的 Python 脚本文件都应在文件头标上 # -*- coding:utf-8 -*- 用于设置编辑器,默认保存为 utf-8 格式. > 注释 ...
- C/C++复杂类型声明
曾经碰到过让你迷惑不解.类似于int * (* (*fp1) (int) ) [10];这样的变量声明吗?本文将由易到难,一步一步教会你如何理解这种复杂的C/C++声明. 我们将从每天都能碰到的较 ...
- OSPF 提升 三 type of Areas
ospf ccnp 三 上图中 rip域中的不连续的100条路由 在a1中导致LSDB太大 在保证网段的可达性的前提下 尽可能减少区域内路由器的lsdb 可以将a1设置 ...
- 牛腩新闻发布系统(五):VS网站发布及常见问题
导读:在千万个回眸中,终于看见了牛腩的归途.好吧,牛腩该整合的都整合完毕了,到了发布的时候了.这时候,不得不再次感慨那句不知道感慨了多少次的感慨:为什么,我要遭遇这么多的坎坷?下面,结合自己的情况,说 ...
- BZOJ 2242 [SDOI2011]计算器 ——EXGCD/快速幂/BSGS
三合一的题目. exgcd不解释,快速幂不解释. BSGS采用了一种不用写EXGCD的方法,写起来感觉好了很多. 比较坑,没给BSGS的样例(LAJI) #include <map> #i ...
- BZOJ 2298: [HAOI2011]problem a【动态规划】
Description 一次考试共有n个人参加,第i个人说:“有ai个人分数比我高,bi个人分数比我低.”问最少有几个人没有说真话(可能有相同的分数) Input 第一行一个整数n,接下来n行每行两个 ...
- angular中ng-repeat去重
[html] view plain copy print?在CODE上查看代码片派生到我的代码片 <div ng-app="myApp" ng-controller=&quo ...
- My97DatePicker 时间控件
<td> <input type="text" id="sendDate" name="sendDate" class=& ...
- 顿悟:Linux是拿来用的,不是拿来折腾的
Linux是拿来用的,而不是折腾其本身.相信这个道理不少聪明人(实用主义者)都明白,然而总是有那么一群人拿Linux去安装各种发行版.研究Linux命令.配置桌面.美化桌面.研究各种wm/DE.永无止 ...
- CentOS 7.5 安装Docker 教程
Docker简介 Docker是一个开源的容器引擎,它有助于更快地交付应用.Docker可将应用程序和基础设施层隔离,并且能将基础设施当作程序一样进行管理. 使用Docker可更快地打包.测试以及部署 ...