[kuangbin]专题四 最短路练习 Tram POJ - 1847【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.
当驾驶员从交叉路口A到交叉路口B进行驾驶时,他/她试图选择将最小化他/她必须手动更换开关的次数的路线。
Write a program that will calculate the minimal number of switch changes necessary to travel from intersection A to intersection B.
编写一个程序,计算从交叉路口A到交叉路口B所需的最小开关变化次数。

【输入】
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.
输入的第一行包含整数N,A和B,由单个空白字符分隔,2 <= N <= 100,1 <= A,B <= N,N是网络中的交叉点数,以及交叉点的编号从1到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.
以下N行中的每一行包含由单个空白字符分隔的整数序列。第i行中的第一个数字Ki(0 <= Ki <= N-1)表示从第i个交叉点出来的轨道数。后面Ki个数字表示直接连接到第i个交叉点的交叉点。第i个交叉点中的交换点最初指向列出的第一个交叉点的方向。

【输出】
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”.
输出的第一行也是唯一一行应包含目标最小数。如果没有从A到B的路由,则该行应包含整数“-1”。

【样例输入】
3 2 1
2 2 3
2 3 1
2 1 2

【样例输出】
0

题目链接:https://cn.vjudge.net/problem/POJ-1847

看了很久不知道题面在说什么,
以样例为例
一共3个点,要从2号点到1号点去
对于1号点,有2条路,如果去2,不需要操作可以直达,如果去3,需要操作一下再去
对于2号点,有2条路,如果去3,不需要操作可以直达,如果去1,需要操作一下再去
对于3号点,有2条路,如果去1,不需要操作可以直达,如果去2,需要操作一下再去
输出从2到1的最小操作数
这样解释完题意就很清楚了,
非常简单的建图+模板题

代码如下:

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
static const int MAXN=100;
struct Node{
    int v,c;
    Node(int _v=0,int _c=0):v(_v),c(_c){}
    bool operator < (const Node &r) const{
        return c>r.c;
    }
};
struct Edge{
    int v,cost;
    Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vector<Edge> E[MAXN+10];
bool vis[MAXN+10];
int dist[MAXN+10];
void dijkstra(int n,int start)
{
    for(int i=1;i<=n;i++)
    {
        vis[i]=false;
        dist[i]=INF;
    }
    priority_queue<Node> Q;
    dist[start]=0;
    Q.push(Node(start,0));
    while(!Q.empty())
    {
        Node tmp=Q.top();
        Q.pop();
        int u=tmp.v;
        if(vis[u])
            continue;
        vis[u]=true;
        for(int i=0;i<E[u].size();i++)
        {
            int v=E[u][i].v;
            int cost=E[u][i].cost;
            if(!vis[v] && dist[v]>dist[u]+cost)
            {
                dist[v]=dist[u]+cost;
                Q.push(Node(v,dist[v]));
            }
        }
    }
}
void addedge(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    int n,a,b;
    cin>>n>>a>>b;
    for(int i=1;i<=n;i++)
    {
        int k;
        cin>>k;
        for(int j=1;j<=k;j++)
        {
            int v;
            cin>>v;
            if(j==1)
                addedge(i,v,0);
            else
                addedge(i,v,1);
        }
    }
    dijkstra(n,a);
    if(dist[b]==INF)
        cout<<-1<<endl;
    else
        cout<<dist[b]<<endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值