POJ 1847 火车变轨道 (简单最短路)

来源:互联网 发布:手机延时软件 编辑:程序博客网 时间:2024/06/06 13:22


题意:火车从一点开到另一点,轨道上有很多岔路口,每个路口都有好几个方向(火车能够选任意一个方向开),但是 默认的是 第一个指向的方向,所以如果要选择别的方向的话得 进行一次切换操作 ,给定一个起点一个终点 ,问最少进行几次 切换操作 能够 使 火车 完成这个历程 , 如果开不到,输出“-1”。


貌似很简单啊,直接把与第一个相连的距离置为0,后面相连的置为1

然后用最短路的方法直接搞.......最短距离就是开关的次数。


不要忘记还有输出-1;(果断忘了一次)



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 12 2 32 3 12 1 2
Sample Output
0




#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<vector>using namespace std;const int N = 111;const int inf = 0x3f3f3f3f;struct P{    int v,c;    P(int v,int c):v(v),c(c){}    P(){}    bool operator < (const P &t1) const    {        return c>t1.c;    }};priority_queue<P>que;vector<P>vec[N];int start,end,n;int dp[N];void dij(){    int i,j;    int u,v,c;    fill(dp,dp+1+n,inf);    dp[start]=0;    que.push(P(start,0));    while(!que.empty()) {        P p = que.top();        que.pop();        u = p.v;        if(dp[u]<p.c) continue;        for(i=0;i<vec[u].size();i++) {            p = vec[u][i];            v = p.v;            if(dp[v]>dp[u]+p.c) {                dp[v] = dp[u] + p.c;                que.push(P(v,dp[v]));            }        }    }    return;}int main(){    int i,j,k,m;    int u,v,c,ans;    scanf("%d%d%d",&n,&start,&end);    for(i=1;i<=n;i++) {        scanf("%d",&m);        for(j=1;j<=m;j++) {            scanf("%d",&v);            if(j==1) vec[i].push_back(P(v,0));            else vec[i].push_back(P(v,1));        }    }    dij();    if(dp[end]==inf) dp[end]=-1;    printf("%d\n",dp[end]);    return 0;}


0 0
原创粉丝点击