Description

  Tram network in Zagreb consists of a number of intersections and rails connecting some of them. In every intersection there is a switch pointing to the one of the rails going out of the intersection. When the tram enters the intersection it can leave only in the direction the switch is pointing. If the driver wants to go some other way, he/she has to manually change the switch.

  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.

 
  题目就是求最短路,然后 建边 i 和 第一个连边,边权为0,其他的边权为1.
 
代码如下:
#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。的更多相关文章

  1. POJ 1847 Tram (最短路径)

    POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...

  2. 最短路 || POJ 1847 Tram

    POJ 1847 最短路 每个点都有初始指向,问从起点到终点最少要改变多少次点的指向 *初始指向的那条边长度为0,其他的长度为1,表示要改变一次指向,然后最短路 =========高亮!!!===== ...

  3. POJ - 1847 Tram(dijkstra)

    题意:有向图有N个点,当电车进入交叉口(某点)时,它只能在开关指向的方向离开. 如果驾驶员想要采取其他方式,他/她必须手动更换开关.当驾驶员从路口A驶向路口B时,他/她尝试选择将他/她不得不手动更换开 ...

  4. poj 1847 Tram

    http://poj.org/problem?id=1847 这道题题意不太容易理解,n个车站,起点a,终点b:问从起点到终点需要转换开关的最少次数 开始的那个点不需要转换开关 数据: 3 2 1// ...

  5. [最短路径SPFA] POJ 1847 Tram

    Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...

  6. POJ 1847 Tram (最短路)

    Tram 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/N Description Tram network in Zagreb ...

  7. poj 1847 Tram【spfa最短路】

    Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12005   Accepted: 4365 Description ...

  8. Floyd_Warshall POJ 1847 Tram

    题目传送门 题意:这题题目难懂.问题是A到B最少要转换几次城市.告诉每个城市相连的关系图,默认与第一个之间相连,就是不用转换,其余都要转换. 分析:把第一个城市权值设为0, 其余设为0.然后Floyd ...

  9. POJ 1847 Tram --set实现最短路SPFA

    题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...

随机推荐

  1. sql高级篇(一)

    1.select top mysql中: select * from persons limit 5; 相当于oracle中的: select * from persons <=5; 在翻页中经 ...

  2. [转]解决LinearLayout中控件不能居右对齐

    在LinearLayout布局时使用右对齐(android:layout_gravity="right")控件对齐方式不生效,需要设置 android:layout_weight= ...

  3. 常用的JS页面跳转代码调用大全

    一.常规的JS页面跳转代码 1.在原来的窗体中直接跳转用 <script type="text/javascript"> window.location.href=&q ...

  4. CentOS安装VirtualBox增强工具

    安装过程中出现错误: Bulding the VirtualBox Guest Additions Kernel modules failedYour system does not seem to  ...

  5. Problem H: 小火山的围棋梦想 多校训练2(小火山专场)

    题目链接:http://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1908 题意:如果'.'被'*'围起来,就把'.'变为'*'. 分析:如果是'*'直接输出, ...

  6. 强制性输出private中变量的三种方法

    众所周知,private里面的变量不可以输出,但是也可以通过特殊途径获得. 1.通过指针暴力内存,把它索罗出来,方法:调试,破掉语法. 并且还可以对类对象进行修改. // Thread.cpp : 定 ...

  7. xshell 图形化连接ubuntu

    原文: http://jingyan.baidu.com/article/d45ad148967fcd69552b80f6.html Xmanager4系列软件是一套非常好的liunx远程操作,尤其是 ...

  8. ZOJ 3939The Lucky Week<模拟/暴力>

    题意:我们认为日期的天数为1,11,21,并且是周一的为Lucky Week;现在给出第一个lucky week的日期,求第N个的lucky week: //1:四百年一轮回,从闰年和平年的判定可以推 ...

  9. Block 进阶

    转载自:http://www.cnblogs.com/xiaofeixiang/p/4666796.html 关于Block之前有一篇文章已经写过一篇文章Object-C-代码块Block回顾,不过写 ...

  10. VS2005混合编译ARM汇编代码-转

    原文地址:http://blog.csdn.net/annelcf/article/details/5468093 公司HW team有人希望可以给他们写一个在WinCE上,单独读写DDR的工具,以方 ...