poj1847Tram 最短路 floyd dijkstra

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的转换次数,若没有路线输出-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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值