hdu 1595 find the longest of the shortest
2012-01-12 02:05
274 查看
要求去除一条路后的最短路的最大长度~~~~挺有意思的~~我的方法很简单,先求出任何一条路都没去时的最短路,那么去掉路后能使最短路变长的路一定在没去时的最短路的集合里面!
这样复杂度直接下降到 O(v*e) ,ac轻而易举~
这样复杂度直接下降到 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 ; } 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; }
相关文章推荐
- HDU 1595 find the longest of the shortest 【枚举删边 + 最短路】
- hdu 1595 find the longest of the shortest (dijkstra + spfa)
- 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(spfa)(存储并枚举最短路径的边)
- HDU 1595 find the longest of the shortest
- hdu 1595 find the longest of the shortest
- find the longest of the shortest HDU1595
- 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(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(spfa)(存储并枚举最短路径的边)
- hdu 1595 find the longest of the shortest (spfa)
- hdu 1595 find the longest of the shortest【最短路+枚举】