HDU 2680 Choose the best route (最短路+反向图)
2014-07-15 15:29
113 查看
题目链接:HDU 2680 Choose the best route
题意:图的大小n*n,有m条路和对应的花费,终点s,有w个起点。问是否存在最短时间从起点到终点的路径;
考虑到有多个起点,如果多次调用Dijkstra会超时,逆向思考,把终点作为起点,只要跑一遍最短路就有答案了。
AC代码:
反思:问题正面很难解决,换个角度可以从反面看待一个问题。
题意:图的大小n*n,有m条路和对应的花费,终点s,有w个起点。问是否存在最短时间从起点到终点的路径;
考虑到有多个起点,如果多次调用Dijkstra会超时,逆向思考,把终点作为起点,只要跑一遍最短路就有答案了。
AC代码:
#include<stdio.h> #include<string.h> const int INF=99999999; int map[1010][1010]; int dis[1010],n; void Dijkstra(int s) { int i,j,t,k; bool vis[1010]; for(i=1;i<=n;i++) { dis[i]=(s==i? 0:map[s][i]); vis[i]=false; } vis[s]=true; dis[s]=0; for(i=2;i<=n;i++) { t=INF; for(j=1;j<=n;j++) { if(!vis[j] && dis[j]<t) { t=dis[j]; k=j; } } if(t==INF) break; vis[k]=true; for(j=1;j<=n;j++) { if(!vis[j] && dis[j]>dis[k]+map[k][j]) { dis[j]=dis[k]+map[k][j]; } } } } int main() { int i,j,a,b,c; int m,s,t,q; while(scanf("%d %d %d",&n,&m,&s)!=EOF) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { map[i][j]=map[j][i]=INF; } } for(i=0;i<m;i++) { scanf("%d %d %d",&a,&b,&c); if(c<map[b][a]) map[b][a]=c; } scanf("%d",&t); int ans=INF; Dijkstra(s); for(i=0;i<t;i++) { scanf("%d",&q); if(ans>dis[q]) ans=dis[q]; } if(ans==INF) printf("-1\n"); else printf("%d\n",ans); } return 0; }
反思:问题正面很难解决,换个角度可以从反面看待一个问题。
相关文章推荐
- HDU 2680 Choose the best route(最短路,反向)
- HDU ~ 2680 ~ Choose the best route (反向建边 or 超级源点 + 最短路)
- HDU 2680 Choose the best route 【最短路 反向建图 dijkstra & SPFA 】
- HDU-2680 Choose the best route(最短路[Dijkstra])
- hdu2680 Choose the best route 最短路,超级起点(多源Dijkstra算法,)
- hdu 2680 Choose the best route【dijstra+反向建图】
- hdu 2680-Choose the best route最短路dijkstra
- hdu 2680 Choose the best route 最短路 解题报告
- HDU 2680 Choose the best route 最短路
- HDU 2680 Choose the best route【最短路】
- HDU 2680 Choose the best route 最短路
- hdu-2680 Choose the best route(最短路)
- hdu 2680 Choose the best route(最短路dijkstra(有重边))
- HdU2680——Choose the best route(单源最短路Bell man-ford)
- HDU 2680 Choose the best route (最短路+迪杰斯特拉)
- hdu 2680 Choose the best route 最短路
- HDU2680 Choose the best route 最短路
- hdu 2680 Choose the best route(最短路)
- HDU 2680 Choose the best route 超级源点or反向建图
- HDU 2680 Choose the best route 变形最短路 DIJKSTRA