hdu 1595 find the longest of the shortest(dijstra + 枚举)
http://acm.hdu.edu.cn/showproblem.php?pid=1595
大致题意:
给一个图。让输出从中删除随意一条边后所得最短路径中最长的。
。
思路:
直接枚举每条边想必是不行的。事实上有些边是不须要枚举的,由于删除它们并不影响起点到终点的最短路。起作用的边都是未删边前的最短路径上的边,删除它们最短距离肯定增大,仅仅需在这些最短距离中求最大的就可以。
记录最短路径上的边,仅仅需一个pre数组记录松弛时每一个点的前驱节点。
#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <string.h>
#define LL long long
#define _LL __int64
using namespace std; const int maxn = 1010;
const int INF = 0x3f3f3f3f; int n,m;
int mapp[1010][1010];
int dis[maxn],vis[maxn];
int pre[maxn],pre1[maxn];
int ans; void init()
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(i == j)
mapp[i][j] = 0;
else
mapp[i][j] = INF;
}
}
memset(pre,-1,sizeof(pre));
} int dijstra(int s)
{
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis)); for(int i = 1; i <= n; i++)
{
dis[i] = mapp[s][i];
pre[i] = s;
}
vis[s] = 1;
pre[s] = -1; for(int i = 1; i < n; i++)
{
int min = INF,pos;
for(int j = 1; j <= n; j++)
{
if(min > dis[j] && !vis[j])
{
min = dis[j];
pos = j;
}
} vis[pos] = 1;
for(int j = 1; j <= n; j++)
{
if(!vis[j] && dis[j] > dis[pos] + mapp[pos][j])
{
dis[j] = dis[pos] + mapp[pos][j];
pre[j] = pos;
}
}
}
return dis[n]; } void solve()
{
memcpy(pre1,pre,sizeof(pre));
ans = -1; int u = n; while(pre1[u] != -1)
{
int tmp = mapp[pre1[u]][u]; mapp[pre1[u]][u] = mapp[u][pre1[u]] = INF; //删除该边 int res = dijstra(1);
if(res != INF)
ans = max(ans,res); mapp[ pre1[u] ][u] = mapp[u][ pre1[u] ] = tmp; u = pre1[u];
}
printf("%d\n",ans);
} int main()
{
int u,v,w;
while(~scanf("%d %d",&n,&m))
{
init(); for(int i = 0; i < m; i++)
{
scanf("%d %d %d",&u,&v,&w);
mapp[u][v] = mapp[v][u] = min(w,mapp[u][v]);
} dijstra(1);
solve();
}
return 0;
}
hdu 1595 find the longest of the shortest(dijstra + 枚举)的更多相关文章
- 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 ...
- 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【次短路】
转载请注明出处:http://blog.csdn.net/a1dark 分析:经典的次短路问题.dijkstra或者SPFA都能做.先找出最短路.然后依次删掉没条边.为何正确就不证明了.了解思想直接A ...
- 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 ...
- 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 ...
- hdu 1595(最短路变形好题)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- HDU 5373(2015多校7)-The shortest problem(模拟%11)
题目地址:pid=5373">HDU 5373 题意:给你一个数n和操作次数t,每次操作将n的各位数之和求出来放在n的末尾形成新的n,问t次操作后得到的n能否够被11整除. 思路:就是 ...
随机推荐
- JavaScript 设计模式之建造者模式
一.建造者模式概念解读 1.建造者模式概念文字解读 建造者模式可以将一个复杂对象的构建与其表示分离,使得同样的构建过程可以创建不同的表示.也就是说如果我们用了建造者模式,那么用户就需要指定需要建造的类 ...
- VI打开和编辑多个文件的命令
http://www.05112.org/school/xtrm/linux/2013/0625/4280.htmlVI打开和编辑多个文件的命令 可分两种情况: 1.在同一窗口中打开多个文件: v ...
- DataGridView在Cell编辑状态响应回车键下的KeyPress/KeyDown/KeyUp事件
我们知道由于DataGridView的单元格DataGridCell处于编辑的时候,当你按Enter键,那么DataGridView是不会激发KewPress/KeyDown/KeyUp这些事件的,因 ...
- .net利用SQLBulkCopy进行数据库之间的大批量数据传递
SQLBulkCopy,用于数据库之间大批量的数据传递.通常用于新,旧数据库之间数据的更新.即使表结构完全不同,也可以通过字段间的对应关系,顺利的将数据导过来. 首先,SQLBulkCopy需要2个连 ...
- 自己动手写shell命令之write
Linux下write命令同意用户跟其它终端上的用户对话.用c语言实现shell命令write.代码例如以下: #include <stdio.h> #include <fcntl. ...
- 外网登陆SAP的两种方式
1.用SAP router实现从外网登录SAPserver(方式一) 自已搭建的局域网结构例如以下: SAPserverIP: 192.168.0.2, 路由器中设为DMZ区 在SAPserver上启 ...
- Disable Oracle Automatic Jobs
By default, Oracle will run some maintance jobs every night. If you don't want to run those jobs, yo ...
- 编译Sqoop2错误解决
Sqoop2的代码结构相对于Sqoop做了大幅度调整,当中编译方式也从ant+maven杂糅的方式变为依赖maven3.今天将源码下下来,放到linux測试机器/export/build下后,执行&q ...
- Parallel Programming AND Asynchronous Programming
https://blogs.oracle.com/dave/ Java Memory Model...and the pragmatics of itAleksey Shipilevaleksey.s ...
- hdoj1114 Piggy-Bank
Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...