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 ...
随机推荐
- mac下删除svn账号
以mac os x为例(Unix/Linux类似), 1.打开命令行窗口,即用户的根目录(用户的home目录) $ ls -al drwxr-xr-x 6 linxyz staff 204 ...
- Windows窗口自动缩放机制
通过自动缩放功能,能使在一个计算机上设计的界面在另一个具有不同分辨率或系统字体的计算机上能正常显示.这样窗体及其控件就能通过智能化调整大小以保障在本地电脑和用户电脑上保持一致. 自动缩放的必要性 如果 ...
- ArcGIS for Flex中引入google map作底图
上篇文章到在ArcGIS View中引入google map,这里讲ArcGIS for Flex中引入google map作底图. 同样道理,以google map作底图,需要编写继承自TiledM ...
- oracle中的查询语句(关于出库入库信息表,明细表,把捆包箱表,单位信息表的集中查询)
--查出所有现金中心的单位IDwith AllUnitas(select t.ORGANIZATIONID orgid,t.parentidfrom CDMS_ORGANIZATION t where ...
- 汉诺塔算法详解之C++
汉诺塔: 有三根杆子A,B,C.A杆上有N个(N>1)穿孔圆环,盘的尺寸由下到上依次变小.要求按下列规则将所有圆盘移至C杆: 每次只能移动一个圆盘: 大盘不能叠在小盘上面. 提示:可将圆盘临时置 ...
- MXS 编辑器外观
Tool > Open User Options File # Give symbolic names to the set of colours used in the standard st ...
- C++温习
string abc; cin >> abc; cin把 空格,制表符,换行符作为终止符,用于提取一个词.如果在abc串中输入的内容是有空格的,流中的内容会被打乱? getline (ci ...
- Easyui修改样式
背景:公司一个后台管理系统做的网站,构架采用Asp.net MVC4.0 + EasyUI, 请了美工做了切图,结果发现美工不懂Easyui,切图的风格又都是直角风格. 而Easyui默认风格是圆角. ...
- html如何绑定radio控件和label控件
只要指定label的"for"属性到radio的id就行,或者用label标签包围住radio. 第一种方式: <input type="radio" i ...
- linq 延迟执行带来的困扰
有这样一个案例: var filteredResult = from f in orgFileList select f; ; i < WorkStatusFilters.ListWorkSta ...