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整除. 思路:就是 ...
随机推荐
- Python 在Visual studio 中做单元测试进行TDD开发
Unit Tests Steve Dower edited this page on 14 Jul · 3 revisions Pages 38 Home Azure Remote Debugging ...
- Linux tr 命令使用
man tr: TR(1) User Commands TR(1) NAME tr - translate or delete characters SYNOPSIS tr [OPTION]... S ...
- Splay入门题目 [HNOI2002]营业额统计
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 这道题貌似很多中做法,我先是用multiset交了一发,然后又写了一发splay. ...
- "V租房"搭建微信租房平台,让租房人发起求租需求并接收合适房源回复,提高租房效率 | 36氪
"V租房"搭建微信租房平台,让租房人发起求租需求并接收合适房源回复,提高租房效率 | 36氪 "V租房"搭建微信租房平台,让租房人发起求租需求并接收合适房源回复 ...
- cobol语言基础培训教程
COBOL 是Common Business Oriented Language 的缩写.它不仅是商业数据处理的理想语言,而且广泛用于数据管理领域,因此COBOL 语言也被称为”用于管理的语言”. 一 ...
- andorid 开发笔记 -- 问题与解决
1. SQLiteDataBase 中 TimeStamp 转化为 Date 的问题:java.text.ParseException: Unparseable date: "Sun Jan ...
- RMAN连接及简单操作
一.RMAN的进入与退出 1.启动RMAN并连接到本地目标数据库 C:\Users\Administrator>set oracle_sid=orcl(如果只有一个实例,则不需要指定,RMAN会 ...
- AIX 常用命令和知识
BOOTLIST:#bootlist -m normal -o (查看bootlist)#bootlist -m normal (设置bootlist为空,谁要在我机器上执行我就要哭了)#boot ...
- C#基础之方法参数
params params 关键字可以指定在参数数目可变处采用参数的方法参数. 在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字 publi ...
- hdu 5586 sum
Problem Description There is a number sequence A1,A2....An,you can select a interval [l,r] or not,al ...