hdu 1595 find the longest of the shortest
2014-04-27 20:11
405 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1595
这道题我用spfa在枚举删除边的时候求最短路超时,改用dijkstra就过了。
View Code
这道题我用spfa在枚举删除边的时候求最短路超时,改用dijkstra就过了。
#include <cstdio> #include <queue> #include <cstring> #include <algorithm> #define maxn 1001 using namespace std; const int inf=1<<30; 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=1; i<=n; i++) dis[i]=(i==1?0:inf); for(int i=1; i<=n; i++) { int x,mm=inf; for(int y=1; y<=n; y++) if(!vis[y]&&dis[y]<mm) mm=dis[x=y]; vis[x]=true; for(int y=1; 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[1]=-1; while(pre[v1]!=-1) { pre1[v1]=pre[v1]; v1=pre[v1]; } int v=n; int max1=-1; while(pre1[v]!=-1) { int mm=g[pre1[v]][v]; g[pre1[v]][v]=g[v][pre1[v]]=inf; dijstra(); if(dis !=inf) max1=max(dis ,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,-1,sizeof(pre)); for(int i=0; i<=n; i++) { for(int j=0; j<=n; j++) { if(i==j) g[i][j]=0; else g[i][j]=inf; } } for(int i=0; i<m; i++) { scanf("%d%d%d",&s,&e,&c); g[s][e]=g[e][s]=min(g[s][e],c); } dijstra(); solve(); } return 0; }
View Code
相关文章推荐
- 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(spfa)(存储并枚举最短路径的边)
- 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 ( spfa + 枚举 )
- 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(最短路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 (枚举+最短路)
- HDU1595 find the longest of the shortest[最短路]