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. cssText笔记

    style.cssText 用来获取/设置元素的样式 设置: <div id= "a" style= "background: red;"> doc ...

  2. Automatic Trading

    Automatic Trading A brokerage firm is interested in detecting automatic trading. They believe that a ...

  3. linux的学习系列 7---管道和过滤器

    有时候,我们可以把两个命令连起来使用,一个命令的输出作为另一个命令的输入,这就叫做管道.为了建立管道,需要在两个命令之间使用竖线(|)连接. 管道是Linux进程之间一种重要的通信机制:除了管道,还有 ...

  4. download下载excel模板的代码

    <%-- 直接在JSP页面中进行文件下载的代码(改 Servlet 或者 JavaBean 的话自己改吧), 支持中文附件名(做了转内码处理). 事实上只要向 out 输出字节就被认为是附件内容 ...

  5. 深入理解linux网络技术-P179

    上锁 net_device结构的组织一节可知,dev_base列表以及dev_name_head和dev_name_index两张hash表由dev_base_list锁保护.然而,该锁只用于对列表和 ...

  6. linker command failed with exit code 1 (use -v to see invocation)修改方法

    它的意思是指文件丢失或者文件重复,你就要仔细查看项目中有没有重复的文件,如有重复修改即可,如果是文件丢失,那就麻烦啦!你需要根据报错的地方进行修补文件,最为直接的方法是重新创建项目.还有另外一种情况是 ...

  7. action参数绑定

    thinkPHP支持操作方法的参数绑定功能 action参数通过直接绑定URL中的变量作为操作方法的参数,可以简化方法的定义甚至路由的简析. 原理是把URL的中参数(不包括模块,控制器和操作名)和控制 ...

  8. oracle 管理

    1.管理数据的用户主要是:sys和system. 区别:(1)sys所有oracle的数据字典的基表和视图都存放在sys用户中,这些基表和视图对于oracle是至关重要的,由数据库自己维护,任何用户都 ...

  9. Java程序员的10道XML面试题 (转)

    包括web开发人员的Java面试在内的各种面试中,XML面试题在各种编程工作的面试中很常见.XML是一种成熟的技术,经常作为从一个平台到其他平台传输数据的标准.XML面试问题包括用于转换XML文件的X ...

  10. java中iofile的路径问题,确定一个未知方法所需要的文件路径

    今天遇到一个极其烦躁的问题,一个jar包中的一个方法,要求函数中要求传入一个String类型的参数,用于指示文件所在的路径.但是对于我们来说完全不知道他需要的路径是绝对路径还是相对路径,所以我尝试了很 ...