POJ 1847 Tram (最短路径)
POJ 1847 Tram (最短路径)
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.
Input
The first line of the input contains integers N, A and B, separated by a single blank character, 2 <= N <= 100, 1 <= A, B <= N, N is the number of intersections in the network, and intersections are numbered from 1 to N.
Each of the following N lines contain a sequence of integers separated by a single blank character. First number in the i-th line, Ki (0 <= Ki <= N-1), represents the number of rails going out of the i-th intersection. Next Ki numbers represents the intersections directly connected to the i-th intersection.Switch in the i-th intersection is initially pointing in the direction of the first intersection listed.
Output
The first and only line of the output should contain the target minimal number. If there is no route from A to B the line should contain the integer "-1".
Sample Input
3 2 1
2 2 3
2 3 1
2 1 2
Sample Output
0
Http
POJ:https://vjudge.net/problem/POJ-1847
Source
最短路径
题目大意
有n个车站,这每一个车站都有若干条出口,开始时指定一个出口,如果走这这个出口,就不要付出代价,否则要付出1的代价,求从1到n的最小代价
解决思路
其实就是最短路径。把指定的出口的边的边权置为0,其他置为1,然后求最短路径即可。
注意无解的情况
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxN=201;
const int maxM=maxN*maxN;
const int inf=2147483647;
int n;
class Graph
{
private:
int cnt;
int Head[maxN];
int W[maxM];
int Next[maxM];
int V[maxM];
//priority_queue<Data,vector<Data>,greater<Data> > Q;
queue<int> Q;
bool inqueue[maxN];
public:
int s,t;
int Dist[maxN];
void init()
{
cnt=0;
memset(Head,-1,sizeof(Head));
memset(Next,-1,sizeof(Next));
}
void Add_Edge(int u,int v,int w)
{
cnt++;
Next[cnt]=Head[u];
Head[u]=cnt;
V[cnt]=v;
W[cnt]=w;
}
void spfa()
{
while (!Q.empty())
Q.pop();
for (int i=1;i<=n;i++)
Dist[i]=inf;
memset(inqueue,0,sizeof(inqueue));
Dist[s]=0;
Q.push(s);
inqueue[s]=1;
do
{
int u=Q.front();
Q.pop();
for (int i=Head[u];i!=-1;i=Next[i])
{
if (Dist[V[i]]>Dist[u]+W[i])
{
Dist[V[i]]=Dist[u]+W[i];
if (inqueue[V[i]]==0)
{
Q.push(V[i]);
inqueue[V[i]]=1;
}
}
}
inqueue[u]=0;
}
while (!Q.empty());
return;
}
void Outp()
{
for (int i=1;i<=n;i++)
{
for (int j=Head[i];j!=-1;j=Next[j])
{
cout<<"("<<i<<","<<V[j]<<") "<<W[j]<<endl;
}
cout<<endl;
}
}
};
Graph G;
int main()
{
G.init();
cin>>n>>G.s>>G.t;
for (int i=1;i<=n;i++)
{
int T;
cin>>T;
int v;
cin>>v;
G.Add_Edge(i,v,0);
if (T<2)
continue;
for (int j=2;j<=T;j++)
{
cin>>v;
G.Add_Edge(i,v,1);
}
}
//G.Outp();
G.spfa();
if (G.Dist[G.t]==inf)
cout<<-1<<endl;
else
cout<<G.Dist[G.t]<<endl;
return 0;
}
POJ 1847 Tram (最短路径)的更多相关文章
- 最短路 || POJ 1847 Tram
POJ 1847 最短路 每个点都有初始指向,问从起点到终点最少要改变多少次点的指向 *初始指向的那条边长度为0,其他的长度为1,表示要改变一次指向,然后最短路 =========高亮!!!===== ...
- [最短路径SPFA] POJ 1847 Tram
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...
- poj 1847 Tram
http://poj.org/problem?id=1847 这道题题意不太容易理解,n个车站,起点a,终点b:问从起点到终点需要转换开关的最少次数 开始的那个点不需要转换开关 数据: 3 2 1// ...
- 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 ...
- (简单) POJ 1847 Tram,Dijkstra。
Description Tram network in Zagreb consists of a number of intersections and rails connecting some o ...
- Floyd_Warshall POJ 1847 Tram
题目传送门 题意:这题题目难懂.问题是A到B最少要转换几次城市.告诉每个城市相连的关系图,默认与第一个之间相连,就是不用转换,其余都要转换. 分析:把第一个城市权值设为0, 其余设为0.然后Floyd ...
- POJ 1847 Tram --set实现最短路SPFA
题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...
- POJ 1847 Tram【Floyd】
题意:给出n个站点,每个站点都有铁路通向其他站点 如果当前要走得路恰好是该站点的开关指向的铁路,则不用扳开关,否则要手动扳动开关,给出起点和终点,问最少需要扳动多少次开关 输入的第一行是n,start ...
随机推荐
- VS中为非控制台程序提供控制台输出窗口
/************************************************************************/ /* 模块名:ConsoleAdapter 文件名 ...
- Centos7下python3安装ipython
一.通过压缩包安装ipython 1.下载ipython安装包 [root@localhost ~]# wget https://pypi.python.org/packages/79/63/b671 ...
- SSIS 你真的了解事务吗?
事务用于处理数据的一致性,事务的定义是,处于同一个事务中的操作是一个工作单元,要么全部执行成功,要么全部执行失败.把事务的概念应用到在实际的SSIS Package场景中,如何在Package中实现事 ...
- Edit Distance问题在两种编程范式下的求解
本文已授权 [Coding博客](https://blog.coding.net) 转载 前言 Edit Distance,中文叫做编辑距离,在文本处理等领域是一个重要的问题,以下是摘自于百度百科的定 ...
- C#_Winform_聊天机器人
最近研究微信公众平台,搭建了一个微信聊天机器人,调用小黄鸡的公众接口,实现在线和小黄鸡聊天的功能. 接口调用不是很麻烦,不过是php版本,所以研究了一下C#的功能模块, Winfrom版 using ...
- centos6 和centos7 安装git 的区别
centos6 和centos7 安装git 的区别 centos6安装git yum install curl-devel expat-devel gettext-devel openssl-dev ...
- 接口自动化学习--testNG
一个月一更的节奏~ testNg是一个开源的自动化测试框架..具体那些什么特点的就不想打了- -,贴张图(虽然也看不懂): 学习网站:https://www.yiibai.com/testng 一样是 ...
- 微软职位内部推荐-Principal Software Eng Mgr
微软近期Open的职位: Job Title:   Principal Software Eng Mgr Work Location: Shanghai, China Job Desc ...
- PAT甲题题解-1009. Product of Polynomials (25)-多项式相乘
多项式相乘 注意相乘结果的多项式要开两倍的大小!!! #include <iostream> #include <cstdio> #include <algorithm& ...
- LeetCode 504. Base 7 (C++)
题目: Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "2 ...