我是靠谱客的博主 可靠时光,这篇文章主要介绍Tram(P1847),现在分享给大家,希望可以做个参考。

题目注意:每一个拐角都是从0开始算,具体看代码的输入部分。


方法:floyd


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define INF 100000000;
int from,to;
#define N 201
int map[N][N];
int n;
int d[N];
int main()
{
freopen("in.txt","r",stdin);
int i,j,k;
cin>>n;
cin>>from>>to;
for (i=1;i<=n;i++)
d[i]= INF ;
d[from]=0;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
{
map[i][j]=(i==j?0:100000000 );
}
for (i=1;i<=n;i++)
{
cin>>k;
//while (k--)
{
for (int cur=1;cur<=k;cur++)
{
cin>>j;
if (cur==1)
map[i][j]=0;
else
map[i][j]=1;
}
}
}
for (k=1;k<=n;k++)
{
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
{
if (map[i][k]+map[k][j]<map[i][j])
map[i][j]=map[i][k]+map[k][j];
}
}
}
if (map[from][to]>=100000000)
cout<<"-1"<<endl;
else
cout<<map[from][to]<<endl;
return 0;
}


dijkstra算法:


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define INF 100000000;
int from,to;
#define N 201
int map[N][N];
int n;
int d[N];
int v[N];
int main()
{
freopen("in.txt","r",stdin);
int i,j,k;
cin>>n;
cin>>from>>to;
for (i=1;i<=n;i++)
d[i]= 100000000 ;
d[from]=0;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
{
map[i][j]=(i==j?0:100000000 );
}
for (i=1;i<=n;i++)
{
cin>>k;
//while (k--)
{
for (int cur=1;cur<=k;cur++)
{
cin>>j;
if (cur==1)
map[i][j]=0;
else
map[i][j]=1;
}
}
}
memset(v,0,sizeof(v));
for (i=1;i<=n;i++)
{
int x,big=100000000;
for (int y=1;y<=n;y++)
{
if (!v[y]&&d[y]<=big)
big=d[x=y];
}
v[x]=1;
for (int y=1;y<=n;y++)
{
d[y]=(d[y]<d[x]+map[x][y]?d[y]:d[x]+map[x][y]);
}
}
if (d[to]>=10000000)
cout<<"-1"<<endl;
else
cout<<d[to]<<endl;
return 0;
}








Tram
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6793 Accepted: 2483

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

Source

Croatia OI 2002 Regional - Juniors

最后

以上就是可靠时光最近收集整理的关于Tram(P1847)的全部内容,更多相关Tram(P1847)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(123)

评论列表共有 0 条评论

立即
投稿
返回
顶部