poj1847 Tram(水题,题意有点难解)

标签:最短路径dijk+邻接表+优先队列
错误:题意理解错误
题解:
3 2 1
2 2 3
2 3 1
2 1 2
n是3,2是a 1是b,从2到1。

  • 第一行从第1个头点出发 有2个节点:接下来有两个尾点。2:第一个尾点2(直接到达)。3:第二个尾点3,要改变方向才能到。
    所以意思就是 1->2 可以直接到达,1->3要改变方向才能到

  • 第二行从第2个头点出发 有2个节点:接下来有两个尾点。3:第一个尾点3(直接到达)。1:第二个尾点1,要改变方向才能到。
    所以意思就是 2->3 可以直接到达,2->1要改变方向才能到

  • 第三行从第3个头点出发 有2个节点:接下来有两个尾点。1:第一个尾点1(直接到达)。2:第二个尾点2,要改变方向才能到。
    所以意思就是 3->1 可以直接到达,3->2要改变方向才能到

Tram

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 21412 Accepted: 7955
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

#include<iostream>
#include<stdio.h>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
int n;
struct node
{
    int v;
    int w;
    bool operator < (const node &b)const//重载小于号
    {
        return w>b.w;//STL库中涉及到排序都是按小于号排的,这里就是把小于号重载为特殊的大于号,所以这里是 >

    }
} now,tmp;

#define INF 0x3f3f3f
vector<node>MAP[500001];
bool vis[500001];
int d[500001];

void dijkstra(int s,int e)
{
    node de;
    memset(d,INF,sizeof(d));
    memset(vis,false,sizeof(vis));
    d[s]=0;
    now.v=s;
    now.w=0;
    priority_queue<node>q;
    q.push(now);
//    for(int i=1; i<=n; i++)
//    {
//        printf("%d ",d[i]);
//    }
//    printf("\n");
    while(!q.empty())
    {
        now=q.top();
//        printf("%d--%d\n",now.v,now.w);
        q.pop();
        if(vis[now.v])//如果已经被访问过,那么跳过
            continue;

        vis[now.v]=true;
        int len=MAP[now.v].size();
        for(int i=0; i<len; i++)
        {
            tmp=MAP[now.v][i];
            if(d[now.v]+tmp.w<d[tmp.v])
            {
                d[tmp.v]=d[now.v]+tmp.w;
                de.v=tmp.v;
                de.w=d[tmp.v];
                q.push(de);
            }
        }
//        for(int i=1; i<=n; i++)
//        {
//            printf("%d ",d[i]);
//        }
//        printf("\n");
    }
}
int main()
{
    int a,b;
    scanf("%d%d%d",&n,&a,&b);
    for(int i=1; i<=n; i++)
    {
        MAP[i].clear();
    }
    for(int j=1; j<=n; j++)
    {
        int m;
        scanf("%d",&m);
        int v,w=0;
        scanf("%d",&v);
        node de;
        de.v=v;
        de.w=w;
        MAP[j].push_back(de);   //v.push_back(t)    在容器的最后添加一个值为t的数据,容器的size变大。
        w=1;
        for(int i=2; i<=m; i++)
        {
            scanf("%d",&v);
            de.v=v;
            de.w=w;
            MAP[j].push_back(de);
        }
    }
//    for(int j=1; j<=n; j++)
//    {
//        int count = MAP[j].size();
//        printf("i=%d\n",j);
//        for (int i = 0; i < count; i++)
//        {
//            printf("%d %d\t",MAP[j][i].v,MAP[j][i].w);
//        }
//        printf("\n");
//    }

    dijkstra(a,b);
    if(d[b]>=INF)
        printf("-1\n");
    else
        printf("%d\n",d[b]);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值