HDU 2680 Choose the best route(Dijkstra,建图的方式很巧妙)
来源:互联网 发布:甲骨文停止java更新 编辑:程序博客网 时间:2024/05/22 01:51
题目地址:点击打开链接
题意:有一个人去他
思路:在HDU2066的基础上变的简单了,那道题是双向图,这道题是单向图,害我wrong了好几发,有2种建图方式(1)把小明的家编号为0,把和他家相邻的车站之间的距离设为0,这样最短距离一定会经过他家相邻的车站(2)反向建图,求从小明朋友家到小明家附近车站的最短距离,这个就和模板差不多了,建图的时候注意反向建图
AC代码1:
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <cstring>#include <climits>#include <cmath>#include <cctype>using namespace std;const int maxn = 1010;const int zui = 1000000000;int map1[maxn][maxn];int lowdist[maxn];int visit[maxn];int n,m,s;void Dijkstra(){ int i,j,k; memset(visit,0,sizeof(visit)); for(i=1; i<=n; i++) { lowdist[i] = map1[0][i]; } for(i=1; i<=n; i++) { int min2 = zui; k = 0; for(j=1; j<=n; j++) { if(!visit[j] && lowdist[j] < min2) { min2 = lowdist[j]; k = j; } } if(k == 0) return; visit[k] = 1; for(j=1; j<=n; j++) { if(!visit[j] && lowdist[k] + map1[k][j] < lowdist[j]) { lowdist[j] = lowdist[k] + map1[k][j]; } } }}int main(){ int i,j; while(scanf("%d%d%d",&n,&m,&s) != EOF) { for(i=0; i<=n; i++) { for(j=0; j<=n; j++) { map1[i][j] = zui; } map1[i][i] = 0; } int p,q,t; for(i=0; i<m; i++) { scanf("%d%d%d",&p,&q,&t); if(t < map1[p][q]) { map1[p][q] = t; } } int w,x; scanf("%d",&w); for(i=0; i<w; i++) { scanf("%d",&x); map1[0][x] = 0; } Dijkstra(); if(lowdist[s] != map1[0][s]) { printf("%d\n",lowdist[s]); } else { printf("-1\n"); } } return 0;}
0 0
- HDU 2680 Choose the best route(Dijkstra,建图的方式很巧妙)
- hdu 2680 Choose the best route (Dijkstra & 反向图)
- HDU 2680 Choose the best route(dijkstra)
- HDU--2680Choose the best route【Dijkstra】
- hdu 2680 Choose the best route (dijkstra)
- HDU 2680 Choose the best route dijkstra
- HDU 2680-Choose the best route(Dijkstra)
- Choose the best route 2680 (dijkstra,反向建图)
- 典型的dijkstra HDU 2680 Choose the best route
- HDU-2680 Choose the best route(最短路[Dijkstra])
- hdu-2680-Choose the best route(dijkstra)
- HDU 2680 Choose the best route(Dijkstra)
- hdu 2680 Choose the best route(spfa/dijkstra)
- hdu 2680 Choose the best route(SPFA或者Dijkstra)
- HDU 2680 Choose the best route 【最短路 反向建图 dijkstra & SPFA 】
- hdu 2680-Choose the best route最短路dijkstra
- HDU 2680 Choose the best route 变形最短路 DIJKSTRA
- HDU 2680 Choose the best route(简单Dijkstra)
- C#打印技术(三)
- jiebaR中文分词,并做词云(R语言)
- Oracle数据库中的数据库名、全局数据库名、实例名、ORACLE_SID
- jdbc数据库操作工具类--DBUtil
- ALM的使用过程及图解
- HDU 2680 Choose the best route(Dijkstra,建图的方式很巧妙)
- 按字节提取整形数值(按位与运算符“&”、右移位运算符“>>”)
- android TextView自动滚动以及Java中改变显示内容
- csrf攻击及其防范介绍
- MATLAB问题之Out of memory. Type HELP MEMORY for your options
- iOS学习日记(第一天)
- C语言中的变量&命名规范
- Android经典底部选项卡集成方式之二
- 二、安装SVN及配置