hdu 2680 Choose the best route(最短路)
来源:互联网 发布:网上怎么开淘宝网店 编辑:程序博客网 时间:2024/06/11 00:14
题目:
链接:点击打开链接
题意:
思路:
自己加一个虚拟源点,把起点集合中的点连接到虚拟点上,并将路径长度设为0,就构成了一个由n+1个点组成的单源最短路算法。
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define INF 100000000const int N = 1010;int n,m,s;int map[N][N],dis[N];int dijkstra(int v){ int vis[N]; memset(vis,0,sizeof(vis)); for(int i=1; i<=n; i++) dis[i] = map[v][i]; dis[v] = 0; for(int i=1; i<n; i++) { int x,minn = INF; for(int y=1; y<=n; y++) { if(!vis[y] && minn > dis[y]) minn = dis[x=y]; } vis[x] = 1; if(minn == INF) break; for(int y=1; y<=n; y++) { if(dis[y] > dis[x] + map[x][y]) dis[y] = dis[x] + map[x][y]; } } return dis[s];}int main(){ //freopen("input.txt","r",stdin); int p,q,t,to; int w; while(scanf("%d%d%d",&n,&m,&s) != EOF) { for(int i=0; i<=n; i++) { for(int j=0; j<=n; j++) map[i][j] = INF; } for(int i=1; i<=m; i++) { scanf("%d%d%d",&p,&q,&t); if(map[p][q] > t) map[p][q] = t; } scanf("%d",&w); for(int i=1; i<=w; i++)//设立虚拟点 { scanf("%d",&to); map[0][to] = 0; } printf("%d\n",dijkstra(0) == INF ? -1 : dijkstra(0)); } return 0;}
----------------------------------------------------------------------
战斗,从不退缩;奋斗,永不停歇~~~~~~~~~~~~~
0 0
- hdu 2680-Choose the best route最短路dijkstra
- HDU 2680 Choose the best route 变形最短路 DIJKSTRA
- HDU 2680Choose the best route(dijk最短路)
- hdu 2680 Choose the best route(最短路)
- 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(最短路[Dijkstra])
- hdu-2680Choose the best route (dijkstra求最短路)
- HDU 2680Choose the best route (最短路+迪杰斯特拉)
- #HDU 2680 Choose the best route 【SPFA最短路】
- 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 最短路 解题报告
- HDU 2680-Choose the best route-最短路+超级源点
- 归并排序
- JAXB完成XML与Java对象的互转
- ListView隔行变色
- GodSon - 关于easyui的窗口和tab页面不执行js说明
- Tomcat下使用war包发布项目
- hdu 2680 Choose the best route(最短路)
- 观察程序的状态,僵尸进程
- 黑马程序员——类加载器
- Mybatis实现【4】-查询解析(一次SQL查询的源码分析)
- (摘自windows核心编程之用内核对象进行线程同步)
- linux 触摸屏驱动中断下半部实现-工作队列
- 解决spring RMI服务端断开导致客户端链接被拒绝问题
- Mybatis实现【5】--事务概述
- hdu 1150 Machine Schedule(二分图求最小点覆盖)