poj1847——Tram(dijkstra)

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 <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <cmath>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXN 1010
#define Mod 10001
using namespace std;
int map[1005][1005],cost[1005][1005],vis[1005],dis[1005],n,m;
int p[MAXN],level[MAXN];
void dijkstra(int s)
{
    int i,j,min,v;
    for(i=1; i<=n; ++i)
    {
        dis[i]=map[s][i];
    }
    vis[s]=1;
    for(i=1; i<=n; ++i)
    {
        min=INF;
        for(j=1; j<=n; ++j)
        {
            if(!vis[j]&&dis[j]<min)
            {
                min=dis[j];
                v=j;
            }
        }
        vis[v]=1;
        for(j=1; j<=n; ++j)
        {
            if(!vis[j]&&map[v][j]<INF)
            {
                if(dis[j]>dis[v]+map[v][j])
                {
                    dis[j]=dis[v]+map[v][j];
                }
            }
        }
    }
}
int main()
{
    int a,b,k,ki;
    while(~scanf("%d%d%d",&n,&a,&b))
    {
        for(int i=1;i<=n;++i)
            for(int j=1;j<=n;++j)
            map[i][j]=INF;
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&k);
            for(int j=0;j<k;++j)
            {
                scanf("%d",&ki);
                if(j==0)
                    map[i][ki]=0;
                else
                    map[i][ki]=1;
            }
        }
        dijkstra(a);
        if(dis[b]<INF)
            printf("%d\n",dis[b]);
        else
            printf("-1\n");
    }
    return 0;
}
时间: 2024-10-08 01:37:28

poj1847——Tram(dijkstra)的相关文章

最短路径算法——迪杰斯特拉算法(Dijkstra)

图结构中应用的最多的就是最短路径的查找了,关于最短路径查找的算法主要有两种:迪杰斯特拉算法(Dijkstra)和Floyd算法. 其中迪杰斯特拉算法(Dijkstra)实现如下: 原理就是不断寻找当前的最优解: void main() { int V[Max][Max]={0,8,32,Infinity,Infinity, 12,0,16,15,Infinity, Infinity,29,0,Infinity,13, Infinity,21,Infinity,0,7, Infinity,Infi

hdu 1874(Dijkstra )

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27692    Accepted Submission(s): 10019 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路

poj1062昂贵的聘礼(Dijkstra**)

1 /* 2 题意: 物主有一个物品,价值为P,地位为L, 以及一系列的替代品Ti和该替代品所对应的"优惠"Vi 3 g[u][i] 表示的是u物品被i物品替换后的优惠价格!(u>0, i>0) 4 g[u][0]表示不用替换该物品的实际价格 ! 5 d[0]表示的是第一个物品经过一系列的物品替换之后的最少优惠价格! 6 7 思路:每当我们通过Dijkstra算法得到离源点(1)最近的距离的节点 p的时候(也就是1...pre[p], p)这条 8 路径上的物品互相替换后得

迪杰斯特拉(dijkstra)算法的简要理解和c语言实现(源码)

迪杰斯特拉(dijkstra)算法:求最短路径的算法,数据结构课程中学习的内容. 1 . 理解 算法思想::设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合(用S表示,初始时S中只有一个源点,以后每求得一条最短路径 , 就将 加入到集合S中,直到全部顶点都加入到S中,算法就结束了),第二组为其余未确定最短路径的顶点集合(用U表示),按最短路径长度的递增次序依次把第二组的顶点加入S中.在加入的过程中,总保持从源点v到S中各顶点的最短路径长度不大于从源点v

hdu 2066 一个人的旅行(Dijkstra)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2066 Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景--草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女--眼看寒假就快到了,这么一大段

算法-迪杰斯特拉算法(dijkstra)-最短路径

迪杰斯特拉算法(dijkstra)-最短路径 简介: 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止. 算法思想: 设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合(用S表示,初始时S中只有一个源点,以后每求得一条最短路径 , 就将加入到集合S中,直到全部顶点都加入到S中

poj 1122 FDNY to the Rescue! (dijkstra)

FDNY to the Rescue! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2475   Accepted: 755 Description The Fire Department of New York (FDNY) has always been proud of their response time to fires in New York City, but they want to make the

单源最短路径(Dijkstra)——贪心算法

Dijkstra算法是解单源最短路径问题的贪心算法.其基本思想是,设置顶点集合点集合S并不断地做贪心选择来扩充这个集合.一个顶点属于集合S当且仅当从源到该顶点的最短路径长度已知.初始时,S中仅含有源.设u是G的其一顶点.把从源到u且中间只经过S中顶点的路称为从源到u的特殊路径,并用数组Distance记录当前每个顶点所对应的最短特殊路径长度.Dijkstra算法每次从V-S中取出具有最短特殊路长度的顶占,Distance就记录了从源到所有其它顶点之间最短路径长度. 例如下图中的有向图,应用Dij

zoj 1750 Idiomatic Phrases Game (dijkstra)

Idiomatic Phrases Game Time Limit: 2 Seconds      Memory Limit: 65536 KB Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters and has a certain meaning. This game will give Tom two idioms. He should bui