hdoj 2680 choose the best route
Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home.
Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes .
Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.
- #include<stdio.h>
- #define INF 0x3f3f3f3f
- #define N 1010
- int vis[N], dis[N], cost[N][N];
- int n, m, s, w, p, q, t;
- int min(int x, int y)
- {
- return x < y ? x : y;
- }
- void dijkstra(int beg)
- {
- int u, v;
- for(u = ; u <= n; u++)
- {
- vis[u] = ;
- dis[u] = INF;
- }
- dis[beg] = ;
- while(true)
- {
- v = -;
- for(u = ; u <= n; u++)
- if(!vis[u] && (v == - || dis[u] < dis[v]))
- v = u;
- if(v == -)
- break;
- vis[v] = ;
- for(u = ; u <= n; u++)
- dis[u] = min(dis[u], dis[v] + cost[v][u]);
- }
- }
- int main()
- {
- int i , j;
- while(~scanf("%d%d%d", &n, &m, &s))
- {
- for(i = ; i <= n; i++)
- for(j = i; j <= n; j++)
- cost[i][j] = cost[j][i] = INF;
- while(m--)
- {
- scanf("%d%d%d", &p, &q, &t);
- if(cost[q][p] > t)
- cost[q][p] = t;
- }
- scanf("%d", &w);
- int sum = INF, b;
- dijkstra(s);
- for(i = ; i <= w; i++)
- {
- scanf("%d", &b);
- if(sum > dis[b])
- sum = dis[b];
- }
- if(sum == INF)
- printf("-1\n");
- else
- printf("%d\n", sum);
- }
- return ;
- }
spfa代码:
- #include <stdio.h>
- #include <string.h>
- #include <queue>
- #define N 10000000
- #define M 1010
- #define INF 0x3f3f3f3f
- using namespace std;
- int n, m, s, cnt;
- int vis[M], head[M], time[M];
- queue<int>q;
- struct node
- {
- int from, to, cost, next;
- }road[N];
- void add(int x, int y, int z)
- {
- node e = {x, y, z, head[x]};
- road[cnt] = e;
- head[x] = cnt++;
- }
- void spfa()
- {
- while(!q.empty())
- {
- int u = q.front();
- q.pop();
- vis[u] = ;
- for(int i = head[u]; i != -; i = road[i].next)
- {
- int v = road[i].to;
- if(time[v] > time[u] + road[i].cost)
- {
- time[v] = time[u] + road[i].cost;
- if(!vis[v])
- {
- vis[v] = ;
- q.push(v);
- }
- }
- }
- }
- }
- int main()
- {
- while(~scanf("%d%d%d", &n, &m, &s))
- {
- memset(head, -, sizeof(head));
- memset(vis, , sizeof(vis));
- memset(time, INF, sizeof(time));
- while(m--)
- {
- int p, q, t;
- scanf("%d%d%d", &p, &q, &t);
- add(p, q, t);
- //add(q, p, t);
- }
- int w;
- scanf("%d", &w);
- while(w--)
- {
- int posi;
- scanf("%d", &posi);
- q.push(posi);
- time[posi] = ;
- vis[posi] = ;
- }
- spfa();
- if(time[s] == INF)
- printf("-1\n");
- else
- printf("%d\n", time[s]);
- }
- return ;
- }
hdoj 2680 choose the best route的更多相关文章
- hdu 2680 Choose the best route
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...
- hdu 2680 Choose the best route (dijkstra算法 最短路问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS ( ...
- HDU 2680 Choose the best route(SPFA)
Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...
- hdu 2680 Choose the best route (dijkstra算法)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2680 /************************************************* ...
- hdu 2680 Choose the best route 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...
- HDU 2680 Choose the best route 最短路问题
题目描述:Kiki想去他的一个朋友家,他的朋友家包括所有的公交站点一共有n 个,一共有m条线路,线路都是单向的,然后Kiki可以在他附近的几个公交站乘车,求最短的路径长度是多少. 解题报告:这道题的特 ...
- HDU 2680 Choose the best route(多起点单终点最短路问题)题解
题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间. 思路:用Floyd超时,Dijkstra遍历,但是也超时.仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转 ...
- Choose the best route(最短路)dijk
http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS (Java/ ...
- HDU2680 Choose the best route 最短路 分类: ACM 2015-03-18 23:30 37人阅读 评论(0) 收藏
Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- Android中RelativeLayout各个属性的含义
android:layout_above="@id/xxx" --将控件置于给定ID控件之上android:layout_below="@id/xxx" - ...
- 53. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
- 性能测试框架Locust初学笔记
Locust初探 Locust是一款类似于Jmeter开源负载测试工具,所不同的是它是用python实现,并支持python脚本. locust提供web ui界面,能够方便用户实时监控脚本运行状态. ...
- C语言初始化——bss段初始化、跃入C、C与汇编
1.bss段初始化 变量 存放位置 初始化的全局变量 数据段 局部变量 栈 malloc函数分配的 堆 未初始的全局变量 bss段 说明:全局变量在未赋初值时,会被保留到bss段. 测试: #incl ...
- iOS开发中的4种数据持久化方式【二、数据库 SQLite3、Core Data 的运用】
在上文,我们介绍了ios开发中的其中2种数据持久化方式:属性列表.归档解档.本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3.Core Data 的运 ...
- vsftpd 修改默认目录
默认配置下: 匿名用户登录 vsftpd 服务后的根目录是 /var/ftp/:系统用户登录 vsftpd 服务后的根目录是系统用户的家目录. 若要修改登录 vsftpd 服务后的根目录,只要修改 / ...
- Linux系统man查询命令等级及意义
1:用户命令,可由任何人启动的 2:系统调用,由内核提供的函数 3:库函数 4:设备,/dev目录下的特殊文件 5:文件格式描述,例如/etc/passwd 6:游戏 7:杂项,例如宏命令包.惯例等 ...
- Apache(ApacheHaus)安装配置教程
1,Apache下载 选择一个版本,点击Download 点击File For Microsoft Windows 由于Apache HTTP Server官方不提供二进制(可执行)的发行版,所以我们 ...
- awk用法
目前虽然有很多工具可以代替awk,但是呢我还是认为awk还是非常重要,比如有时候load数据到hive,mysql发现数据有点问题,这样可以先对比文件和库中数据是否一致,这样awk就发挥用处了,还有从 ...
- 三级联动---DropDownList控件
AutoPostBack属性:意思是自动回传,也就是说此控件值更改后是否和服务器进行交互比如Dropdownlist控件,若设置为True,则你更换下拉列表值时会刷新页面(如果是网页的话),设置为fl ...