hdu 1595 find the longest of the shortest
http://acm.hdu.edu.cn/showproblem.php?pid=1595
这道题我用spfa在枚举删除边的时候求最短路超时,改用dijkstra就过了。
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define maxn 1001
using namespace std;
const int inf=<<; int g[maxn][maxn];
int n,m;
int s,e,c;
int dis[maxn];
bool vis[maxn];
int pre[maxn],pre1[maxn]; /*void spfa()
{
queue<int>q;
memset(vis,false,sizeof(vis));
for(int i=1; i<=n; i++) dis[i]=inf;
dis[1]=0;
vis[1]=true;
q.push(1);
pre[1]=-1;
while(!q.empty())
{
int u=q.front(); q.pop();
vis[u]=false;
for(int i=1; i<=n; i++)
{
if(dis[i]>dis[u]+g[u][i]&&g[u][i]!=inf)
{
dis[i]=dis[u]+g[u][i];
pre[i]=u;
if(!vis[i])
{
q.push(i);
vis[i]=true;
}
}
}
}
}*/ void dijstra()
{
memset(vis,false,sizeof(vis));
for(int i=; i<=n; i++) dis[i]=(i==?:inf);
for(int i=; i<=n; i++)
{
int x,mm=inf;
for(int y=; y<=n; y++) if(!vis[y]&&dis[y]<mm) mm=dis[x=y];
vis[x]=true;
for(int y=; y<=n; y++)
{
if(dis[y]>dis[x]+g[x][y]&&!vis[y])
{
pre[y]=x;
dis[y]=dis[x]+g[x][y];
}
}
}
}
void solve()
{
int v1=n;
pre1[]=-;
while(pre[v1]!=-)
{
pre1[v1]=pre[v1];
v1=pre[v1];
}
int v=n;
int max1=-;
while(pre1[v]!=-)
{
int mm=g[pre1[v]][v];
g[pre1[v]][v]=g[v][pre1[v]]=inf;
dijstra();
if(dis[n]!=inf)
max1=max(dis[n],max1);
g[pre1[v]][v]=g[v][pre1[v]]=mm;
v=pre1[v];
}
printf("%d\n",max1);
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(pre,-,sizeof(pre));
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(i==j) g[i][j]=;
else g[i][j]=inf;
}
}
for(int i=; i<m; i++)
{
scanf("%d%d%d",&s,&e,&c);
g[s][e]=g[e][s]=min(g[s][e],c);
}
dijstra();
solve();
}
return ;
}
hdu 1595 find the longest of the shortest的更多相关文章
- 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(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 ...
- 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整除. 思路:就是 ...
随机推荐
- Handler处理长时间事件
当我们在处理一些比较长时间的事件时候,比如读取网络或者数据库的数据时候,就要用到Handler,有时候为了不影响用户操作应用的流畅还要开多一个线程来区别UI线程,在新的线程里面处理长时间的操作.开发的 ...
- hdu5017:补题系列之西安网络赛1011
补题系列之西安网络赛1011 题目大意:给定一个椭球: 求它到原点的最短距离. 思路: 对于一个椭球的标准方程 x^2/a^2 + y^2/b^2 +z^2/c^2=1 来说,它到原点的最短距离即为m ...
- 什么是空间复杂度(What is actually Space Complexity ?)
属于空间复杂度(Space Complexity)在很多情况下被错认为是附属空间(Auxiliary Space),下面是附属空间和空间复杂度的定义. 附属空间(Auxiliary Space)是算法 ...
- [HEOI 2013 day2] SAO (树形动态规划)
题目大意 给一棵N个节点的有向树(N <= 1000),求其拓扑序列个数. 思路 我们将任意一个点作为根,用dp[i][j]表示以节点i为根的子树满足节点i在第j个位置上的拓扑序列的个数.在求节 ...
- Decorator学习笔记
初学者,自己的理解,请各位前辈不吝指正! Decorator,装饰模式,设计模式之一,谈谈我的理解,装饰这个词在我概念中就是给某个事物加上一些美丽的外表,把它变得更加完美.但是装饰是可以随时改变的,可 ...
- RequireJS入门(一)
RequireJS由James Burke创建,他也是AMD规范的创始人. RequireJS会让你以不同于往常的方式去写JavaScript.你将不再使用script标签在HTML中引入JS文件,以 ...
- pyqt搜索指定信息 github处找到,谢谢这位朋友的帮助了
def tabunqi(self,text): #第一遍添加之后,不提示,当第二次添加相同的数据时,就提示下 text1=str(text) items = self.downwid ...
- 学习selenium所须要具备的技术
学习selenium所须要具备的知识或技术 1.selenium进行的自己主动化測试是基于ui层面的,所以html,css,javascript基本上是不可缺少的,至于javascript,有非常多的 ...
- Unity 触屏缩放模型
现在的手机都是触屏控制的,那么在游戏中我们想通过手指在屏幕上滑动捕获相应的动作呢?Unity官网API中提供了Input类和Touch类,在该类里提供了许多接口.相信只要我们稍微看下,就可以自己应用了 ...
- Linux查看系统状态及备份
1. 如何看当前Linux系统有几颗物理CPU和每颗CPU的核数?cat /proc/cpuinfo将CPU的总核数除以物理CPU的个数,得到每颗CPU的核数.2. 查看系统负载有两个常用的命令,是哪 ...