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整除. 思路:就是 ...
随机推荐
- Java优化技巧
过早的优化是万恶之源. 优化了的代码可读性变差,可改性可适应性变差,可维护性变差. 远离过度优化,优化是个无底洞,把主要精力放在代码逻辑上. 优化的代码是活在当下的,是严重依赖硬件的,不利于表达永恒的 ...
- DBA_实践指南系列11_Oracle Erp R12性能调优基础(案例)
http://www.itpub.net/thread-1412437-1-1.html
- 总结Codeigniter的一些优秀特性
总结Codeigniter的一些优秀特性 近期准备接手改进一个别人用Codeigniter写的项目.尽管之前也实用过CI,可是是全然按着自己的意思写的,没按CI的一些套路.用在公众的项目,不妨按框架规 ...
- oracle字符串处理函数--待整理
http://www.cnblogs.com/xd502djj/archive/2010/08/11/1797577.html http://blog.csdn.net/qq373591361/art ...
- Correlation and Regression
Correlation and Regression Sample Covariance The covariance between two random variables is a statis ...
- AI、机器学习、深度学习、神经网络
1.AI:人工智能(Artificial Intelligence) 2.机器学习:(Machine Learning, ML) 3.深度学习:Deep Learning 人工功能的实现是让机器自己学 ...
- angular学习笔记(十五)-module里的'服务'
本篇介绍angular中的模块:module 在笔记(二)http://www.cnblogs.com/liulangmao/p/3711047.html里已经讲到过模块,这篇主要讲模块的 '服务' ...
- ny737 石子合并(一) 总结合并石子问题
描述: 在一个圆形操场的四周摆放着n 堆石子.现要将石子有次序地合并成一堆. 规定每次只能选相邻的2 堆石子合并成新的一堆,并将新的一堆石子数记为该次合并的得分. 试设计一个算法,计算出将n堆石子合并 ...
- Objective-C的“多继承”——消息转发
Objective-C的“多继承”——消息转发 当单继承不够用,很难为问题域建模时,我们通常都会直接想到多继承.多继承是从多余一个直接基类派生类的能力,可以更加直接地为应用程序建模.但是Objecti ...
- Oracle中文字符乱码?设置Oracle客户端字符编码与服务端一致
经常可能出现查询Oracle数据时,中文显示乱码,这很可能是因为,Oracle服务端的字符编码与客户端不一致引起的. 这时,我们需要做的是,如何设置自己的客户端字符编码与服务端一致. 查询Oracle ...