(简单) POJ 1847 Tram,Dijkstra。
Description
When a driver has do drive from intersection A to the
intersection B he/she tries to choose the route that will minimize the
number of times he/she will have to change the switches manually.
Write a program that will calculate the minimal number of
switch changes necessary to travel from intersection A to intersection
B.
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=;
const int INF=10e8; int vis[MaxN]; void Dijkstra(int lowcost[],int cost[][MaxN],int N,int start)
{
int minn,minp; for(int i=;i<=N;++i)
{
lowcost[i]=INF;
vis[i]=;
}
lowcost[start]=; for(int cas=;cas<=N;++cas)
{
minn=INF;
minp=-;
for(int i=;i<=N;++i)
if(!vis[i] && lowcost[i]<minn)
{
minn=lowcost[i];
minp=i;
} if(minp==-)
return;
vis[minp]=; for(int i=;i<=N;++i)
if(!vis[i] && cost[minp][i]!=- && lowcost[i]>lowcost[minp]+cost[minp][i])
lowcost[i]=lowcost[minp]+cost[minp][i];
}
} int map1[MaxN][MaxN];
int ans[MaxN];
int N,A,B; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int k,a; for(int i=;i<=MaxN;++i)
for(int j=;j<=MaxN;++j)
map1[i][j]=-; scanf("%d %d %d",&N,&A,&B); for(int i=;i<=N;++i)
{
scanf("%d",&k); if(k==)
continue; scanf("%d",&a); map1[i][a]=;
--k; while(k--)
{
scanf("%d",&a); if(map1[i][a]==-)
map1[i][a]=;
}
} //for(int i=1;i<=N;++i)
// for(int j=1;j<=N;++j)
// cout<<map1[i][j]<<' '; Dijkstra(ans,map1,N,A); printf("%d\n",ans[B]==INF ? - : ans[B]); return ;
}
(简单) POJ 1847 Tram,Dijkstra。的更多相关文章
- POJ 1847 Tram (最短路径)
POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...
- 最短路 || POJ 1847 Tram
POJ 1847 最短路 每个点都有初始指向,问从起点到终点最少要改变多少次点的指向 *初始指向的那条边长度为0,其他的长度为1,表示要改变一次指向,然后最短路 =========高亮!!!===== ...
- POJ - 1847 Tram(dijkstra)
题意:有向图有N个点,当电车进入交叉口(某点)时,它只能在开关指向的方向离开. 如果驾驶员想要采取其他方式,他/她必须手动更换开关.当驾驶员从路口A驶向路口B时,他/她尝试选择将他/她不得不手动更换开 ...
- poj 1847 Tram
http://poj.org/problem?id=1847 这道题题意不太容易理解,n个车站,起点a,终点b:问从起点到终点需要转换开关的最少次数 开始的那个点不需要转换开关 数据: 3 2 1// ...
- [最短路径SPFA] POJ 1847 Tram
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...
- POJ 1847 Tram (最短路)
Tram 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/N Description Tram network in Zagreb ...
- poj 1847 Tram【spfa最短路】
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12005 Accepted: 4365 Description ...
- Floyd_Warshall POJ 1847 Tram
题目传送门 题意:这题题目难懂.问题是A到B最少要转换几次城市.告诉每个城市相连的关系图,默认与第一个之间相连,就是不用转换,其余都要转换. 分析:把第一个城市权值设为0, 其余设为0.然后Floyd ...
- POJ 1847 Tram --set实现最短路SPFA
题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...
随机推荐
- sql高级篇(一)
1.select top mysql中: select * from persons limit 5; 相当于oracle中的: select * from persons <=5; 在翻页中经 ...
- [转]解决LinearLayout中控件不能居右对齐
在LinearLayout布局时使用右对齐(android:layout_gravity="right")控件对齐方式不生效,需要设置 android:layout_weight= ...
- 常用的JS页面跳转代码调用大全
一.常规的JS页面跳转代码 1.在原来的窗体中直接跳转用 <script type="text/javascript"> window.location.href=&q ...
- CentOS安装VirtualBox增强工具
安装过程中出现错误: Bulding the VirtualBox Guest Additions Kernel modules failedYour system does not seem to ...
- Problem H: 小火山的围棋梦想 多校训练2(小火山专场)
题目链接:http://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1908 题意:如果'.'被'*'围起来,就把'.'变为'*'. 分析:如果是'*'直接输出, ...
- 强制性输出private中变量的三种方法
众所周知,private里面的变量不可以输出,但是也可以通过特殊途径获得. 1.通过指针暴力内存,把它索罗出来,方法:调试,破掉语法. 并且还可以对类对象进行修改. // Thread.cpp : 定 ...
- xshell 图形化连接ubuntu
原文: http://jingyan.baidu.com/article/d45ad148967fcd69552b80f6.html Xmanager4系列软件是一套非常好的liunx远程操作,尤其是 ...
- ZOJ 3939The Lucky Week<模拟/暴力>
题意:我们认为日期的天数为1,11,21,并且是周一的为Lucky Week;现在给出第一个lucky week的日期,求第N个的lucky week: //1:四百年一轮回,从闰年和平年的判定可以推 ...
- Block 进阶
转载自:http://www.cnblogs.com/xiaofeixiang/p/4666796.html 关于Block之前有一篇文章已经写过一篇文章Object-C-代码块Block回顾,不过写 ...
- VS2005混合编译ARM汇编代码-转
原文地址:http://blog.csdn.net/annelcf/article/details/5468093 公司HW team有人希望可以给他们写一个在WinCE上,单独读写DDR的工具,以方 ...