poj 1847 Tram(最短路径)

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个点 接下来的N行分别为点i的情况:第一个数字k表示与该点连通的点的个 数,接下来输入k个数,表示与点i相连的点的编号,第一个所连的点为不用 手动改而直接通过,其余的点通过的话要改一次,求从A点到B点改扳手的最小次数。


最短路问题:

(1)Dijkstr算法

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int map[101][101];
int N,A,B;
int  Dijkstr(int u,int v)
{
    bool vis[101];
    int dist[101];
    for (int i=1; i<=N; i++)
    {
        dist[i]=map[u][i];
        vis[i]=false;
    }
    vis[u]=true;
    dist[u]=0;
    for (int i=1; i<=N; i++)
    {
        int k=-1;
        int Min=1000000;
        for (int j=1; j<=N; j++)
        {
            if(dist[j]<Min && !vis[j])
            {
                Min=dist[j];
                k=j;
            }
        }
        vis[k]=true;
        for (int j=1; j<=N; j++)
        {
            if(!vis[j] && Min+map[k][j]<dist[j])
            {
                dist[j]=Min+map[k][j];
            }
        }
    }
    return dist[v]==1000000? -1:dist[v];
}
int main()
{
    int k;
    while (scanf("%d%d%d",&N,&A,&B)!=EOF)
    {
        for (int i=0; i<=N; i++)
        {
            for (int j=0; j<=N; j++)
            {
                map[i][j]=1000000;
            }
        }
        for (int i=1; i<=N; i++)
        {
            int n;
            scanf("%d",&k);
            if(k)
            {
                scanf("%d",&n);
                map[i][n]=0;
            }
            for (int j=1; j<k; j++)
            {
                scanf("%d",&n);
                map[i][n]=1;
                
            }
        }
        printf("%d\n",Dijkstr(A,B));
    }
    return 0;
}

(2)spfa算法

#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int N,A,B,cnt;
struct node
{
    int u,v,w;
    int next;
}Edge[10001];
int head[10001];
void add(int u,int v,int w)
{
    Edge[cnt].u=u;
    Edge[cnt].w=w;
    Edge[cnt].v=v;
    Edge[cnt].next=head[u];
    head[u]=cnt++;
}
int spfa(int u,int m)
{
    bool vis[101]={0};
    int dist[101];
    priority_queue<int,vector<int>,less<int> >q;
    for (int i=1; i<=N; i++)
    {
        dist[i]=100000;
    }
    dist[u]=0;
    vis[u]=true;
    q.push(u);
    while (!q.empty())
    {
        int u=q.top();
        q.pop();
        vis[u]=false;
        for (int i=head[u]; i!=-1; i=Edge[i].next)
        {
            int v=Edge[i].v;
            if(dist[v]>dist[u]+Edge[i].w)
            {
                dist[v]=dist[u]+Edge[i].w;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=true;
                }
            }
        }
    }
    return dist[m]==100000?-1:dist[m];
}
int main()
{
    int k;
    while (scanf("%d%d%d",&N,&A,&B)!=EOF)
    {
        cnt=0;
        memset(head, -1, sizeof(head));
        for (int i=1; i<=N; i++)
        {
            int n;
            scanf("%d",&k);
            if(k)
            {
                scanf("%d",&n);
                add(i,n,0);
            }
            for (int j=1; j<k; j++)
            {
                scanf("%d",&n);
                add(i,n,1);
            }
        }
        printf("%d\n",spfa(A,B));
    }
    return 0;
}

(3)floyd算法

#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
int map[101][101];
int N,A,B;
int main()
{
    int k;
    while (scanf("%d%d%d",&N,&A,&B)!=EOF)
    {
        for (int i=0; i<=N; i++)
        {
            for (int j=0; j<=N; j++)
            {
                map[i][j]=1000000;
            }
        }
        for (int i=1; i<=N; i++)
        {
            int n;
            scanf("%d",&k);
            if(k)
            {
                scanf("%d",&n);
                map[i][n]=0;
            }
            for (int j=1; j<k; j++)
            {
                scanf("%d",&n);
                map[i][n]=1;
                
            }
        }
        for (int k=1; k<=N; k++)
        {
            for (int i=1; i<=N; i++)
            {
                for (int j=1; j<=N; j++)
                {
                    if(map[i][k]+map[k][j]<map[i][j])
                    {
                        map[i][j]=map[i][k]+map[k][j];
                    }
                }
            }
        }
        printf("%d\n",map[A][B]==1000000?-1:map[A][B]);
    }
    return 0;
}

(4)dfs

#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int N,A,B,cnt;
struct node
{
    int v,w;
    int next;
}Edge[10001];
int head[101];
void add(int u,int v,int w)
{
    Edge[cnt].w=w;
    Edge[cnt].v=v;
    Edge[cnt].next=head[u];
    head[u]=cnt++;
}
bool vis[101];
int Min;
void dfs(int a,int sum)
{
    if(sum>=Min )//剪枝
    {
        return;
    }
    if(a==B)
    {
        if(Min>sum)
        {
            Min=sum;
        }
        vis[a]=false;
        return;
    }
    for (int i=head[a]; i!=-1; i=Edge[i].next)
    {
        if(!vis[Edge[i].v])
        {
            vis[a]=true;
            dfs(Edge[i].v, sum+Edge[i].w);
        }
    }
    vis[a]=false;
}
int main()
{
    int k;
    while (scanf("%d%d%d",&N,&A,&B)!=EOF)
    {
        cnt=0;
        Min=100000;
        memset(head, -1, sizeof(head));
        memset(vis, false, sizeof(vis));
        for (int i=1; i<=N; i++)
        {
            int n;
            scanf("%d",&k);
            if(k)
            {
                scanf("%d",&n);
                add(i,n,0);
            }
            for (int j=1; j<k; j++)
            {
                scanf("%d",&n);
                add(i,n,1);
            }
        }
        vis[A]=true;
        dfs(A, 0);
        if(Min==100000)
        {
            Min=-1;
        }
        printf("%d\n",Min);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值