N - Tram POJ - 1847
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
题意:n个点嘛,然后同时n个边嘛。然后求a到b的最小改变方向的次数。
第一行从第1个头点出发 2:接下来有两个尾点。2:第一个尾点2(直接到达)。3:第二个尾点3,要改变方向才能到。所以意思就是 1->2 可以直接到达,1->3要改变方向才能到。
思路:要改方向的路径权值为1,直接到的0,到不了的inf。求最短路。
我就纳闷了,Switch in the i-th intersection is initially pointing in the direction of the first intersection listed. 这句话这么就代表第一个数可以直接到达,之后的数必须转方向才能到达。
代码:
#include <iostream>
#include <queue>
#include <cstring>
#include <stdio.h>
#include <math.h>
#define inf 0x3f3f3f
using namespace std;
typedef struct node {
int v,date;
} ty;
bool operator < ( const ty &a, const ty &b )
{
return a.date>b.date;
}
int a[102][102];
int dis[102];
int via[102];
int n,m1,m2;
ty t,d;
void dijkstra( int v0 )
{
priority_queue <ty> Q;
memset(dis,inf,sizeof(dis));
dis[v0] = 0;
t.v = v0;
t.date = 0;
Q.push(t);
while ( !Q.empty() ) {
if ( via[Q.top().v]==1 ) {
Q.pop();
continue ;
}
t = Q.top();
Q.pop();
int u = t.v;
via[u] = 1;
dis[u] = t.date;
for ( int i=1; i<=n; i++ ) {
if ( via[i]==0 && a[u][i]<inf && dis[i]>dis[u]+a[u][i] ) {
dis[i] = dis[u]+a[u][i];
d.v = i;
d.date = dis[i];
Q.push(d);
}
}
}
if ( dis[m2]>=100056 ) { // 它怎么也不等于inf,我也不知道为啥,只好让他小于一个大值了
cout << "-1" << endl;
}
else {
cout << dis[m2] << endl;
}
}
int main()
{
cin >> n >> m1 >> m2;
memset(via,0,sizeof(via));
memset(a,inf,sizeof(a));
for ( int i=1; i<=n; i++ ) {
a[i][i] = 0;
}
for ( int i=1; i<=n; i++ ) {
int x,y;
cin >> x;
int isp = 1;
while ( x-- ) {
cin >> y;
if ( isp==1 ) {
a[i][y] = 0;
isp = 0;
}
else {
a[i][y] = 1;
}
}
}
dijkstra(m1);
return 0;
}