Tram
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 21783 | Accepted: 8107 |
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
题意是输入 n,a,b 三个数, 表示有 n 个点 (1-n), 起点是 a, 终点是 b, 然后接下来有 n 行, 每一行的第一个数 m 表示后面将会有 m 个数, 输入结构是这样的, 然后我再具体的解释一下.
3 2 1 3 表示共有 n 个点, 接下来有 n 行, 2 表示起点, 1 表示终点
2 2 3 第一个数 2 表示后面有 2 个数, 因为这是第 1 行, 所以后面两个数表示从 1 到 2 和从 1 到 3 的边
2 3 1 表示从 2 到 3 和从 2 到 1 的边
2 1 2 表示从 3 到 1 和从 3 到 2 的边
因为每个点都有开关, 第一个点是不需要开开关的, 但是之后的每一个都要开开关, 意思是输入 m 后的第一条边是不需要开开关的, 后面的 2-m 的边都是需要开开关的, 比如样例的第二行, 2 2 3 就是 1-2 这条边不需要开开关, 1-3 这条边需要开开关. 这道题最后问的就是从 a 点到 b 点, 最少需要开多少个开关, 题意很难读懂, 懂了以后这道题就很简单了, 把用不用开开关的作为边的权值, 不用开开关的权值为 0, 用开开关的边权值为 1, 然后跑个 dij 就好了.
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- //const int maxn = 1e5+5;
- #define ll long long
- ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
- ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
- #define inf 0x3f3f3f3f
- #define MAX INT_MAX
- #define FOR(i,a,b) for( int i = a;i <= b;++i)
- #define bug cout<<"--------------"<<endl
- using namespace std;
- int n,m,tot,A,B;
- int ver[11000],edge[11000],next[11000],head[11000],vis[11000],d[11000];
- void add(int x,int y,int z)
- {
- ver[++tot] = y,edge[tot] = z,next[tot] = head[x],head[x] = tot;
- }
- void dijiestra()
- {
- priority_queue<pair<int,int>>que;
- memset(vis,0,sizeof(vis));
- memset(d,inf,sizeof(d));
- d[A] = 0;
- que.push(make_pair(0,A));
- while(que.size())
- {
- int x = que.top().second;que.pop();
- if(vis[x] == 1) continue;
- vis[x] = 1;
- for(int i= head[x];i;i=next[i])
- {
- int y = ver[i],z = edge[i];
- if( d[y]> d[x] + z )
- {
- d[y] = d[x] + z;
- que.push(make_pair(-d[y],y));
- }
- }
- }
- }
- int main()
- {
- iOS::sync_with_stdio(false);
- cin>>n>>A>>B;
- FOR(i,1,n)
- {
- int m,one;
- cin>>m>>one;
- add(i,one,0);
- FOR(j,2,m)
- {
- int two;
- cin>>two;
- add(i,two,1);
- }
- }
- dijiestra();
- if(d[B] == inf) cout<<"-1"<<endl;
- else cout<<d[B]<<endl;
- }
来源: http://www.bubuko.com/infodetail-3160456.html