HDU 1595 find the longest of the shortest 删掉任意一条边的最长最短路
来源:互联网 发布:录音音乐合成软件 编辑:程序博客网 时间:2024/06/06 04:21
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1595
题意
某个男的背着她女朋友找小三,她女朋友决定去男的所在城市报复。已知路上存在一条正在修复的路,她女朋友一定会走最短的路去找他,且不会走在修复的路。现在这个男的想评估一下这条最短路最大可以是多少,以腾出更多的时间。。
思路
1.这题图可以是完全图
2.删除的边应该在最短路径上删除,如果删除不在最短路上的边,那么最短路不变。
3.用SPFA超时,大概是因为是稠密图,参考文章:Dijkstra、Bellman_Ford、SPFA、Floyd算法复杂度比较
#include<cstdio>#include<queue>#include<iostream>#include<vector>#include<map>#include<cstring>#include<string>#include<set>#include<stack>#include<algorithm>#define cle(a) memset(a,0,sizeof(a))#define inf(a) memset(a,0x3f,sizeof(a))#define ll long long#define Rep(i,a,n) for(int i=a;i<=n;i++)using namespace std;#define INF2 9223372036854775807llconst int INF = ( 2e9 ) + 2;const ll maxn = 1010;const int maxm = maxn*maxn/2;int vis[maxn],d[maxn],pre[maxn];int mp[maxn][maxn];/*TLEint SPFA(int s,int n,bool f){ memset(vis,0,sizeof(vis)); for(int i=1; i<=n; i++)d[i]=INF; d[s]=0; vis[s]=1; queue<int> q; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(int i=1; i<=n; i++) if(mp[u][i]&&d[i]>d[u]+mp[u][i]) { d[i]=d[u]+mp[u][i]; if(!vis[i]) { vis[i]=1; q.push(i); } if(f) pre[i]=u; } } return d[n];}*///ACint dijkstra(int s,int n,bool f){ memset(vis,0,sizeof(vis)); for(int i=1; i<=n; i++)d[i]=INF; d[s]=0; for(int i=1; i<=n; i++) { int u=-1,mn=INF; for(int j=1; j<=n; j++) if(d[j]<mn&&!vis[j]) { mn=d[j]; u=j; } if(u==-1)break; vis[u]=1; for(int v=1; v<=n; v++) if(mp[u][v]&&d[v]>d[u]+mp[u][v]) { d[v]=d[u]+mp[u][v]; if(f) pre[v]=u; } } return d[n];}int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { memset(mp,0,sizeof(mp)); memset(pre,-1,sizeof(pre)); for(int i=1; i<=m; i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); mp[u][v]=mp[v][u]=w; } int ans=dijkstra(1,n,1); int cur=n; while(pre[cur]!=-1) { int temp=mp[cur][pre[cur]]; mp[cur][pre[cur]]=mp[pre[cur]][cur]=0; int t=dijkstra(1,n,0); if(t!=INF) ans=max(ans,t); mp[cur][pre[cur]]=mp[pre[cur]][cur]=temp; cur=pre[cur]; } printf("%d\n",ans); }}
阅读全文
0 0
- HDU 1595 find the longest of the shortest 删掉任意一条边的最长最短路
- HDU 1595 find the longest of the shortest 删掉任意一条边的最长最短路
- HDU 1595 find the longest of the shortest枚举+最短路(删掉任意一条边的最长最短路)★★
- HDU 1595 find the longest of the shortest 删除一条边的最长最短路
- HDU 1595 find the longest of the shortest (最短路)
- HDU 3986 Harry Potter and the Final Battle 删掉任意一条边的最长最短路
- HDU - 1595 find the longest of the shortest(最短路Dijkstra+枚举删边)
- HDU 1595 find the longest of the shortest (最短路+记录路径+枚举删边)
- HDU 3986 Harry Potter and the Final Battle 删掉任意一条边的最长最短路(有重边)
- HDU 1595 find the longest of the shortest【次短路】
- HDU 1595 find the longest of the shortest(枚举,最短路)
- hdu 1595 find the longest of the shortest(最短路spfa)
- Hdu 1595 find the longest of the shortest 枚举+最短路
- 【HDU】1595 find the longest of the shortest 枚举+最短路
- hdu 1595 find the longest of the shortest 最短路dijkstra+枚举
- hdu 1595 find the longest of the shortest(最短路+枚举)
- hdu 1595 find the longest of the shortest【最短路+枚举】
- HDU1595 find the longest of the shortest(最短路)
- 深入Kotlin
- JavaBean
- 神经网络知识点汇总——FNN
- git diff 参数的差异
- 其实这个是个疑问,不知道提问区在什么地方(可能是个傻问题)
- HDU 1595 find the longest of the shortest 删掉任意一条边的最长最短路
- maven profile 使用
- 笔记--xml简介
- Linux 学习笔记(一)
- 服务器中判断客户端socket断开连接的方法
- PHP使用Redis存储
- POJ1035-Spell checker(字符串,模拟)
- 大整数乘法
- Zookeeper学习笔记 --- Python操作zookeeper