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. call_grant_dml.sql

    set echo offpromptprompt =========================================================================== ...

  2. EditText的一点深入的了解

    最近在开发android下的记事本程序时,频繁的使用EditText控件,折腾来折腾去,算是对其的了解更深入了一些.特将这些收获记录如下: 一.几个属性的介绍 android:gravity=&quo ...

  3. 《JavaScript高级程序设计》读书笔记 ---变量

    ECMAScript 的变量是松散类型的,所谓松散类型就是可以用来保存任何类型的数据.换句话说,每个变量仅仅是一个用于保存值的占位符而已.定义变量时要使用var 操作符(注意var 是一个关键字),后 ...

  4. for循环与foreach

    注意点: for循环时,可以对集合进行操作,但foreach循环中,对集合进行操作会报错:                  for 循环可以获取下标 如下代码: package com.tt;imp ...

  5. J2SE基本数据结构

    1.J2SE中的常用数据结构对象的继承关系如下图 Collection ........|--------List ........|..........|----------ArrayList .. ...

  6. PAT (Advanced Level) 1112. Stucked Keyboard (20)

    找出一定没问题的字符(即一连串的额字符x个数能被k整除的),剩下的字符都是可能有问题的. #include<cstdio> #include<cstring> #include ...

  7. 【转】调用getActionBar()报Call requires API level 11 (current min is 8): android.app.Activity#getActionBar

    解决办法: 第一种方法:修改AndroidManifest.xml中的minSdkVersion=11   第二种方法: 1.导入android-support-v7-appcompat项目,并将其作 ...

  8. 认识和选用常用的几种 GPRS 模块(转)

    源:http://blog.sina.com.cn/s/blog_4d80055a0100e8kr.html 我在这里把常见的GPRS模块分成3种: (1)GPRS DTU(GPRS数传单元,常称GP ...

  9. css盒子

    <html><head lang="en"> <meta charset="UTF-8"> <title>< ...

  10. android 1.6 launcher研究之自定义ViewGroup (转 2011.06.03(二)——— android 1.6 launcher研究之自定义ViewGroup )

    2011.06.03(2)——— android 1.6 launcher研究之自定义ViewGroup2011.06.03(2)——— android 1.6 launcher研究之自定义ViewG ...