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 ...
随机推荐
- ptrace注入型病毒“聊天剽窃手”分析
概述 “聊天剽窃手”Windseeker是一款间谍软件,它使用了ptrace进程注入技术,能够对微信和QQ的聊天记录进行监控. 软件安装后的桌面图标和启动界面如图所示: 行为分析 该应用首先获取手 ...
- Servlet——提交表单信息,Servlet之间的跳转
HTML表单标签:<form></form> 属性: actoion: 提交到的地址,默认为当前页面 method: 表单提交方式 有get和post两种方式,默认为get ...
- [Oracle][Corruption]究竟哪些检查影响到 V$DATABASE_BLOCK_CORRUPTION
根据 471716.1,11g 之后,下列动作如果遇到坏块,都会输出记录到 V$DATABASE_BLOCK_CORRUPTION. - Analyze table .. Validate str ...
- springboot项目生成jar包(带静态资源)方法
[Maven]在pom.xml文件中使用resources插件的小作用 不过war包比较实用,毕竟独立的tomcat比较好控制
- GitHub 新手教程 五,Git GUI 新手教程(2),Clone Existing Repository 克隆代码库
1,注意: 网上大部分教程都是从“Create New Repository”讲起,这其实给我们新手埋了很大的一个坑.按照类似的教程,仅做到一半,各种错误窗口就会弹出来了,像什么:“非同一代码库”.“ ...
- leetcode-递增的三元子序列
给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1,使得 arr[ ...
- PAT甲题题解-1014. Waiting in Line (30)-模拟,优先级队列
题意:n个窗口,每个窗口可以排m人.有k为顾客需要办理业务,给出了每个客户的办理业务时间.银行在8点开始服务,如果窗口都排满了,客户就得在黄线外等候.如果有一个窗口用户服务结束,黄线外的客户就进来一个 ...
- #Linux第四周学习总结——扒开系统调用的三层皮(上)
Linux第四周学习总结--扒开系统调用的三层皮(上) 一.用户态.内核态和中断 系统调用通过库函数. 1.用户态和内核态 区分(不同的指令执行级别): 用户态:在相应的低执行状态下,代码的掌控范围受 ...
- Beta版本冲刺(二)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...
- Eclipse使用较多的快捷键
快速修正:Ctrl+1 单词补全:Alt+/ 快速Outline:Ctrl+O 删除行:Ctrl+D 选中到行末/行首:Shift+End/Home 注释:Ctrl+/ 变为大/小写:Ctrl+Shi ...