HDU2680 Choose the best route(最短路)
来源:互联网 发布:windows ftp登录命令 编辑:程序博客网 时间:2024/06/05 08:47
【题目链接】
http://acm.hdu.edu.cn/showproblem.php?pid=2680
题目意思
奇奇想去她朋友家,但她晕车先尽量少坐车,所以请你求出最短坐车距离。奇奇家附近有多个车站点,(也就是起点有多个)
解题思路
最短的模板题,唯一区别在起点不在是一个而是多个,所以只要存下起点编号,在初始起点时候全部初始掉就可以了。具体看代码
代码部分
#include <bits/stdc++.h>using namespace std;#define LL long longconst int maxn=1005;const int INF=0x3f3f3f;int n,m,s,w;int a[maxn][maxn]; ///地图int dis[maxn],d[maxn]; ///d数组存起始车站int vis[maxn];int dist (){ int mi,j; for (int i=0;i<w;i++) ///初始全部起始点 { vis[d[i]]=1; dis[d[i]]=0; if (d[i]==s) ///起始点等于终点 return dis[s]; for (int k=1;k<=n;k++) { if (dis[k]>a[d[i]][k]) dis[k]=a[d[i]][k]; } } for (int i=0;i<n-w;i++) ///剩下n-w个车站最短路 { mi=INF; for (int k=1;k<=n;k++) ///寻找最近的车站 { if (!vis[k]) { if (dis[k]<mi) { mi=dis[k]; j=k; } } } vis[j]=1; for (int k=1;k<=n;k++) ///更新能以新加入的车站迭代的车站 { if (!vis[k]) { if (dis[k]>dis[j]+a[j][k]) { dis[k]=dis[j]+a[j][k]; } } } if (j==s) return dis[s]; } return -1;}int main(){ while (scanf("%d %d %d",&n,&m,&s)!=EOF) { memset(a,INF,sizeof(a)); memset(vis,0,sizeof(vis)); memset(dis,INF,sizeof(dis)); for(int i=0;i<m;i++) { int x,y,t; scanf("%d %d %d",&x,&y,&t); if (a[x][y]>t) { a[x][y]=t; } } scanf("%d",&w); for (int i=0;i<w;i++) scanf("%d",&d[i]); cout<<dist()<<endl; }}
阅读全文
0 0
- hdu2680最短路Choose the best route
- HDU2680 Choose the best route 最短路
- hdu2680 Choose the best route 最短路(Dijkstra算法)
- HDU2680 Choose the best route(最短路)
- HDU2680 Choose the best route (最短路)(Dijkstra算法)
- hdu2680 Choose the best route 最短路,超级起点(多源Dijkstra算法,)
- hdu2680 Choose the best route(spfa求最短路模板题)
- HDu2680(Choose the best route)
- hdu2680 Choose the best route (dijkstra)
- HDU2680 Choose the best route
- hdu2680 Choose the best route
- HDU2680-Choose the best route
- HDU2680 Choose the best route
- hdu2680 choose the best route
- HDU2680-Choose the best route(经典最短路问题dijkstra算法)
- hdu2680—Choose the best route(spfa)
- hdu2680 Choose the best route (dijkstra)
- Choose the best route hdu2680 Dijkstra
- 【TensorFlow】数据处理(将MNIST转为TFRecord)
- java.lang.Double转成String
- Gym
- deepmind_lab 运行ASSETS 需要下面的东西
- Multiples of 3 and 5
- HDU2680 Choose the best route(最短路)
- 什么是事件分发线程
- 元胞自动机 C实现 + OpenCV界面
- d3.js-选择集与数据
- python3—3一些常见的细节处理
- IntelliJ IDEA启动Tomcat后,无法访问Tomcat主页
- django入门一(整体流程)
- AOP使用方法
- 第5章 目录配置规范