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 ...
随机推荐
- Oracle常用查询语句
"ORACLE数据字典视图的种类分别为:USER,ALL 和 DBA. USER_*:有关用户所拥有的对象信息,即用户自己创建的对象信息 ALL_*:有关用户可以访问的对象的信息,即用户自己 ...
- Python基础数据类型之字符串
Python基础数据类型之字符串 一.Python如何创建字符串 在python中用引号将一些文本包起来就构成了字符串(引号可以是单引号.双引号.单三引号,双三引号,它们是完全相同的) >> ...
- 【LeetCode】ZigZag Conversion(Z 字形变换)
这道题是LeetCode里的第6道题. 题目要求: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ...
- dp的两个不错的题
C - Cheapest Palindrome Keeping track of all the cows can be a tricky task so Farmer John has instal ...
- PTA 09-排序2 Insert or Merge (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/675 5-13 Insert or Merge (25分) According to ...
- 03-for循环in遍历
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- nginx报错 too many open files in system
系统进不去了,用ssh连接服务器也非常慢,负载均衡显示后端连接异常,重启mysql数据库,发现经常重启,或者直接关机,访问页面也访问不到. http://www.51testing.com/html/ ...
- iOS开发工具篇-AppStore统计工具
苹果官方的iTunes Connect提供的销售数据统计功能比较弱,例如只能保存最近30天的详细销售数据,界面丑陋, 无法查看App的排名历史变化情况等. 早有一些公司提供了专门的解决方案或工具.这些 ...
- HDU——2112HDU Today(SPFA+简单Hash或map+前向星)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- [luoguP2324] [SCOI2005]骑士精神(A*?)
传送门 蒟蒻并不懂A*是什么,但是题解里有个Astar 可以看出,当前棋盘和最终的棋盘如果有k个不同的,那么至少需要k-1步来移动 所以如果 当前步数 + k - 1 > limit 就直接退出 ...