HDU 1595 find the longest of the shortest
2013-04-08 08:25
197 查看
做法就是先求出最短路径,然后枚举每一条最短路的边,每次枚举一条删除掉求最短路,求出最大的长度就是答案了
#include <iostream> #include <cstdio> #include <memory.h> #include <functional> #include <queue> #include <vector> using namespace std; #define pii pair<int,int> const int maxn=1010; int g[maxn][maxn],dis[maxn],path[maxn],n,m; bool vis[maxn]; vector<int>pv; void printPath(int v){ if(v==-1){ return; } printPath(path[v]); pv.push_back(v); } int dijkstra(int s){ priority_queue<pii,vector<pii>,greater<pii> >q; memset(dis,0X3F,sizeof(dis)); memset(vis,0,sizeof(vis)); memset(path,0,sizeof(path)); dis[s]=0; path[s]=-1; q.push(make_pair(0,s)); while (q.size()) { pii t=q.top(); q.pop(); if(t.second==1)return dis[1]; else if(vis[t.second])continue; vis[t.second]=1; for (int i=1;i<=n;++i) { if(g[t.second][i]&&dis[i]>dis[t.second]+g[t.second][i]&&!vis[i]){ path[i]=t.second; dis[i]=dis[t.second]+g[t.second][i]; q.push(make_pair(dis[i],i)); } } } } int main(){ while (scanf("%d%d",&n,&m)==2) { int ans=0; memset(g,0,sizeof(g)); pv.clear(); for (int i=0;i<m;++i) { int u,v,w; scanf("%d%d%d",&u,&v,&w); if(g[u][v]==0){ g[u][v]=g[v][u]=w; }else{ g[u][v]=g[v][u]=min(g[u][v],w); } } dijkstra(n); printPath(1); for (int i=0;i<pv.size()-1;++i) { int t=g[pv[i]][pv[i+1]]; g[pv[i]][pv[i+1]]=g[pv[i+1]][pv[i]]=0;//删除 ans=max(ans,dijkstra(n)); g[pv[i]][pv[i+1]]=g[pv[i+1]][pv[i]]=t;//还原 } printf("%d\n",ans); } return 0; }
相关文章推荐
- 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(spfa)(存储并枚举最短路径的边)
- hdu 1595 find the longest of the shortest
- hdu 1595 find the longest of the shortest(dijstra + 枚举)
- 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
- 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(dijstra + 枚举)
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- HDU 1595 find the longest of the shortest(枚举,最短路)
- find the longest of the shortest HDU - 1595(最短路)
- 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 + spfa)