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. 直接用postman测试api ,服务器端没提供跨域也可以访问。

    1. 直接用postman测试api ,服务器端没提供跨域也可以访问. 但是,如果用本地的 sever 搭的server, 然后去访问api的话,浏览器会提示 跨域错误.

  2. UIActivityIndicatorView的详细使用(加载菊花)

    UIActivityIndicatorView实例提供轻型视图,这些视图显示一个标准的旋转进度轮.当使用这些视图时,最重要的一个关键词是小.20×20像素是大多数指示器样式获得最清楚显示效果的大小.只 ...

  3. C++调用C#之C++DLL调用C# COM控件

    1. 新建项目 这里我们使用ATL,来接受C# COM控件向外发送的事件. 2. 初始化ATL #include "stdafx.h" CComModule _module; BO ...

  4. php 四种基础算法 ---- 选择排序法

    2. 选择排序法: 选择排序法思路: 每次选择一个相应的元素,然后将其放到指定的位置 代码: function select_sort($arr) {//实现思路 双重循环完成,外层控制轮数,当前的最 ...

  5. mysql Group By

    1.概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组,所谓的分组就是将一个“数据集”划分成若干个“小区域”,然后针对若干个“小区域”进行数据处理. 2.原始表 3.简 ...

  6. 默认系统为UEFI启动的GPT分区的WIN7(8),如何安装VHD的UEFI WIN8(7)

    默认系统为UEFI启动的GPT分区的WIN7(8),如何安装VHD的UEFI WIN8(7) 情况A:如果默认系统为UEFI启动.GPT分区的WIN7,想安装个VHD的UEFI WIN8.1 1:系统 ...

  7. 解析GenericOptionsParser

    hadoop源代码分析(4)-org.apache.hadoop.util包-GenericOptionsParser类[原创]   一  准备 hadoop版本:1.0.3,GenericOptio ...

  8. POJ 3307 Smart Sister

    先找出所有的数,排序,然后o(1)效率询问 #include<cstdio> #include<cstring> #include<cmath> #include& ...

  9. git使用之如何将github库下载到本地与如何将代码上传github

    git使用之如何将github库下载到本地与如何将代码上传github ---------------------------------------------------------------- ...

  10. Entity Framework 学习初级篇6--EntityClient

    System.Data.EntityClient 命名空间是 实体框架的 .NET Framework 数据提供程序.EntityClient 提供程序使用存储特定的 ADO.NET 数据提供程序类和 ...