Tram
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 11183 | Accepted: 4096 |
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.
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.
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
题意:火车从一点开到另一点,轨道上有很多岔路口,每个路口都有好几个方向(火车能够选任意一个方向开),但是火车默认的是第一个指向的方向,如果选择别的方向需要 进行一次切换操作 ,给定一个起点一个终点 ,问最少进行几次 切换操作 能够 使 火车从起点到达终点 , 若无法到达输出“-1”。 思路:设默认路径边权为0,备选路径边权为1,求单源最短路即可。
dijkstra:0ms
#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f
#define MAX 100+10
using namespace std;
int n, s, e;
int dist[MAX], map[MAX][MAX], vis[MAX];
void init()
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(i == j)
map[i][j] = 0;
else
map[i][j] = INF;
}
}
}
void getmap()
{
int num;
int i, j, x;
for(int i = 1; i <= n; i++)
{
scanf("%d", &num);
for(j = 0; j < num; j++)
{
scanf("%d", &x);
if(j == 0)
map[i][x] = 0;
else
map[i][x] = 1;
}
}
}
void dijkstra()
{
int i, j;
int next, Min;
for(i = 1; i <= n; i++)
{
vis[i] = 0;
dist[i] = map[s][i];
}
vis[s] = 1;
for(i = 2; i <= n; i++)
{
Min = INF;
for(j = 1; j <= n; j++)
{
if(!vis[j] && Min > dist[j])
{
Min = dist[j];
next = j;
}
}
vis[next] = 1;
for(j = 1; j <= n; j++)
{
if(!vis[j] && dist[next] + map[next][j] < dist[j])
dist[j] = dist[next] + map[next][j];
}
}
if(dist[e] == INF)
printf("-1\n");
else
printf("%d\n", dist[e]);
}
int main()
{
while(scanf("%d%d%d", &n, &s, &e) != EOF)
{
init();
getmap();
dijkstra();
}
return 0;
}
floyd:16ms
#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f
#define MAX 100+10
using namespace std;
int n, s, e;
int dist[MAX], map[MAX][MAX], vis[MAX];
void init()
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(i == j)
map[i][j] = 0;
else
map[i][j] = INF;
}
}
}
void getmap()
{
int num;
int i, j, x;
for(int i = 1; i <= n; i++)
{
scanf("%d", &num);
for(j = 0; j < num; j++)
{
scanf("%d", &x);
if(j == 0)
map[i][x] = 0;
else
map[i][x] = 1;
}
}
}
void floyd()
{
int k, i, j;
for(k = 1; k <= n; k++)
{
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
map[i][j] = min(map[i][j], map[i][k]+map[k][j]);
}
}
}
if(map[s][e] == INF)
printf("-1\n");
else
printf("%d\n", map[s][e]);
}
int main()
{
while(scanf("%d%d%d", &n, &s, &e) != EOF)
{
init();
getmap();
floyd();
}
return 0;
}
spfa:32ms
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define INF 0x3f3f3f
#define MAX 100+10
using namespace std;
struct Edge
{
int to, val, next;
}edge[1010];
int n, s, e;
int dist[MAX], vis[MAX], top, head[MAX];
queue<int> Q;
void init()
{
top = 0;
for(int i = 1; i <= n; i++)
{
vis[i] = 0;
head[i] = -1;
}
}
void addedge(int a, int b, int d)
{
edge[top].to = b;
edge[top].val = d;
edge[top].next = head[a];
head[a] = top++;
}
void getmap()
{
int num;
int i, j, x;
for(i = 1; i <= n; i++)
{
scanf("%d", &num);
for(j = 0; j < num; j++)
{
scanf("%d", &x);
if(j == 0)
addedge(i, x, 0);
else
addedge(i, x, 1);
}
}
}
void spfa()
{
int i;
for(i = 1; i <= n; i++)
dist[i] = INF;
Q.push(s);
vis[s] = 1;//标记
dist[s] = 0;
while(!Q.empty())
{
int u = Q.front();
Q.pop();
vis[u] = 0;//清除标记
for(i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(dist[v] > dist[u] + edge[i].val)
{
dist[v] = dist[u] + edge[i].val;
if(!vis[v])
{
vis[v] = 1;
Q.push(v);
}
}
}
}
if(dist[e] == INF)
printf("-1\n");
else
printf("%d\n", dist[e]);
}
int main()
{
while(scanf("%d%d%d", &n, &s, &e) != EOF)
{
init();
getmap();
spfa();
}
return 0;
}