hdu 1595 find the longest of the shortest
来源:互联网 发布:火狐量子浏览器 知乎 编辑:程序博客网 时间:2024/05/17 20:31
要求去除一条路后的最短路的最大长度~~~~挺有意思的~~我的方法很简单,先求出任何一条路都没去时的最短路,那么去掉路后能使最短路变长的路一定在没去时的最短路的集合里面!
这样复杂度直接下降到 O(v*e) ,ac轻而易举~
#include<iostream>#include<vector>#include<algorithm>#include<string>#include<map>#include<queue>using namespace std;const int maxn=1111;const int inf=0x3f3f3f3f;struct zz{ int from; int to; int cost; int id;}zx,tz;vector<zz>g[maxn];vector<int>v;queue<int>q;int go[maxn];int back[maxn];int id[maxn];bool vis[maxn];int n,m,tmax;void spfa(){ memset(vis,false,sizeof(vis)); memset(back,0,sizeof(back)); memset(id,0,sizeof(id)); for(int i=1;i<=n;i++) { go[i]=inf; } while(!q.empty()) { q.pop(); } go[1]=0; q.push(1); vis[1]=true; int now,to,cost; while(!q.empty()) { now=q.front(); q.pop(); for(int i=0;i<g[now].size();i++) { to=g[now][i].to; cost = g[now][i].cost+go[now]; if(cost < go[to] ) { go[to]=cost; back[to]=now; id[to]=g[now][i].id; if(!vis[to]) { vis[to]=true; q.push(to); } } } vis[now]=false; } int temp=n; while(back[temp]) { v.push_back(id[temp]); temp=back[temp]; } return ; }int spfa1(int tid){ int temp,now,to,cost,t; while(!q.empty()) { q.pop(); } memset(vis,false,sizeof(vis)); for(int i=1;i<=n;i++) { go[i]=inf; } go[1]=0; q.push(1); vis[1]=true; while(!q.empty()) { now=q.front(); q.pop(); for(int i=0;i<g[now].size();i++) { if(g[now][i].id == tid) { continue; } to=g[now][i].to; cost=g[now][i].cost + go[now]; if(cost<go[to]) { go[to]=cost; if(!vis[to]) { vis[to]=true; q.push(to); } } } vis[now]=false; } return go[n];}int spfa2(){ tmax=0; int temp; for(int i=0;i<v.size();i++) { temp=spfa1(v[i]); if(temp > tmax ) { tmax=temp; } } return tmax; }int main(){ while(cin>>n>>m) { for(int i=0;i<=n;i++) { g[i].clear(); } v.clear(); for(int i=1;i<=m;i++) { cin>>zx.from>>zx.to>>zx.cost; zx.id=i; g[zx.from].push_back(zx); swap(zx.from,zx.to); g[zx.from].push_back(zx); } spfa(); cout<<spfa2()<<endl; } return 0;}
- find the longest of the shortest HDU
- find the longest of the shortest HDU
- HDU 1595 find the longest of the shortest
- hdu 1595 find the longest of the shortest
- HDU 1595 find the longest of the shortest
- 1595 hdu 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
- 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(Dijkstra)
- HDU 1595 find the longest of the shortest
- HDU 1595 find the longest of the shortest
- HDU 1595 find the longest of the shortest (最短路)
- 使用C#格式化字符串
- Unix网络编程代码 第8章 基本UDP套接字编程
- c#连接sql数据库的一个处理类
- Xcode 4 创建静态库
- 【二分图多重匹配】POJ 2112
- hdu 1595 find the longest of the shortest
- kvm中的base_image妙用
- ubuntu 11.10下装opencv2.3
- 读写器reader和writer详解
- 数据集的Store
- 小问题集。。。
- ubuntu下android开发如何识别真实手机
- ubuntu安装软件或者更新时提示:waiting for jockey-backend exit 解决办法
- ubuntu菜单面板被误删或右下角回收站丢失解决办法