poj1847Tram 最短路 floyd dijkstra

来源:互联网 发布:淘宝借贷利息 编辑:程序博客网 时间:2024/06/04 19:43
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


题意: 第一行n个点,求a到b的转换次数,若没有路线输出-1.

            接下来n行,第i行第一个数字表示第i个点可以转换点的个数t,接下来t个数,第一个数(ai)表示从i到ai自动转换为0,其他为手动转换为1


 最近脑子有问题,经常看不懂题,崩溃。。。

1.floyd

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int M=110;const int inf=0x3f3f3f3f;int cost[M][M];int n,a,b;int main(){    int t,lu,i,j;    while(~scanf("%d%d%d",&n,&a,&b))    {        memset(cost,inf,sizeof(cost));        for( i=1;i<=n;i++)            cost[i][i]=0;        for(i=1;i<=n;i++)        {            scanf("%d",&t);            for(j=1;j<=t;j++)            {                scanf("%d",&lu);                if(j==1)                    cost[i][lu]=0;                else                    cost[i][lu]=1;            }        }        for(int k=1;k<=n;k++)        for(i=1;i<=n;i++)            for(j=1;j<=n;j++)            cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);        if(cost[a][b]==inf)            printf("-1\n");        else        printf("%d\n",cost[a][b]);    }    return 0;}


2. dijkstra


#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int M=110;const int inf=0x3f3f3f3f;int cost[M][M];int n,a,b;int vis[M];int dis[M];void dijstra(int st){    memset(vis,0,sizeof(vis));    for(int i=1;i<=n;i++)        dis[i]=cost[st][i];    vis[st]=1;    for(int i=2;i<=n;i++)    {        int mini=inf,k;        for(int j=1;j<=n;j++)        {            if(!vis[j]&&mini>dis[j])            {                mini=dis[j];                k=j;            }        }        vis[k]=1;        for(int u=1;u<=n;u++)        {            if(!vis[u]&&dis[u]>dis[k]+cost[k][u])               dis[u]=dis[k]+cost[k][u];        }    }}int main(){    int t,lu,i,j;    while(~scanf("%d%d%d",&n,&a,&b))    {        for( i=1;i<=n;i++)            for(j=1;j<=n;j++)        {            if(i==j)                cost[i][j]=0;            else                cost[i][j]=inf;        }        for(i=1;i<=n;i++)        {            scanf("%d",&t);            for(j=1;j<=t;j++)            {                scanf("%d",&lu);                if(j==1)                    cost[i][lu]=0;                else                    cost[i][lu]=1;            }        }        dijstra(a);        if(dis[b]==inf)            printf("-1\n");        else        printf("%d\n",dis[b]);    }    return 0;}


原创粉丝点击