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 ...
随机推荐
- firefox与IE对js和CSS的区别(转http://log-cd.javaeye.com/blog/548665)
? "700px" : document.body.clientWidth>1000 ? "1000px" : "auto");// ...
- logstash 1.5.3 配置使用redis做续传
logstash是ELK中的一员大将, redis插件也是<The Logstash Book>中介绍的一个很好用的玩意儿. 之前,用比较小的集群部署的时候,没有介入redis中间件,所以 ...
- C# 文件大小
/// <summary> /// 获取文件大小 /// </summary> /// <param name="sFullName">< ...
- Android将Activity 打 jar包 (解决资源文件不能打包的问题)
转载地址:http://blog.csdn.net/xiaanming/article/details/9257853 最近有一个需要,我们公司做了一个apk客户端,然后其他的公司可以根据自己的需要来 ...
- 六、通过插件如何创建自己的MEL command
1. MAYA API支持不同类型的plugin (1)Command Plugin——扩充MEL命令 (2)Tool Commands——通过鼠标输出 (3)DG plugin——对场景添加新的操作 ...
- 树莓派_360wifi2_佳能MP236打印机
入手树莓派后一直没时间弄,设想用360wifi做无线网卡,也一直不得解,今天成功写下经验 本人刷的是官方系统,版本为3.12.28,首先更新系统内核,参考以下网址: http://groenholdt ...
- PHP指定字段的多维数组排序方法
PHP数组排序可以用array_multisort方法实现,但是如果是多维数组,并且我们要指定数组中的某个字段进行排序,那么这就需要我们自己写方法实现了. function sortArrByFiel ...
- 【EF学习笔记12】----------解释查询和本地查询 区分 Enumerable 和 Queryable
简单介绍:Enumerable 和 Queryable 他们都是静态类,位于命名控件 System.Linq下,分别为IEnumerable<T>和IQueryable<T>提 ...
- C#winform如何最小化主窗口
1.如果不想让程序在任务栏中显示,请把窗体的属性ShowInTaskbar设置为false;2.如果想让程序启动时就最小化,请设置窗体的属性WindowState设置为Minimized.(Minim ...
- ios delegate, block, NSNotification用法
ios中实现callback可以通过两种方法,委托和NSNotification 委托的话是一对一的关系,例如一个UIViewController里有一个tableView, 将该viewContro ...