(简单) POJ 1847 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.

  题目就是求最短路,然后 建边 i 和 第一个连边,边权为0,其他的边权为1.

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

const int MaxN=110;
const int INF=10e8;

int vis[MaxN];

void Dijkstra(int lowcost[],int cost[][MaxN],int N,int start)
{
    int minn,minp;

    for(int i=1;i<=N;++i)
    {
        lowcost[i]=INF;
        vis[i]=0;
    }
    lowcost[start]=0;

    for(int cas=1;cas<=N;++cas)
    {
        minn=INF;
        minp=-1;
        for(int i=1;i<=N;++i)
            if(!vis[i] && lowcost[i]<minn)
            {
                minn=lowcost[i];
                minp=i;
            }

        if(minp==-1)
            return;
        vis[minp]=1;

        for(int i=1;i<=N;++i)
            if(!vis[i] && cost[minp][i]!=-1 && lowcost[i]>lowcost[minp]+cost[minp][i])
                lowcost[i]=lowcost[minp]+cost[minp][i];
    }
}

int map1[MaxN][MaxN];
int ans[MaxN];
int N,A,B;

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int k,a;

    for(int i=1;i<=MaxN;++i)
        for(int j=1;j<=MaxN;++j)
            map1[i][j]=-1;

    scanf("%d %d %d",&N,&A,&B);

    for(int i=1;i<=N;++i)
    {
        scanf("%d",&k);

        if(k==0)
            continue;

        scanf("%d",&a);

        map1[i][a]=0;
        --k;

        while(k--)
        {
            scanf("%d",&a);

            if(map1[i][a]==-1)
                map1[i][a]=1;
        }
    }

    //for(int i=1;i<=N;++i)
    //    for(int j=1;j<=N;++j)
    //        cout<<map1[i][j]<<‘ ‘;

    Dijkstra(ans,map1,N,A);

    printf("%d\n",ans[B]==INF ? -1 : ans[B]);

    return 0;
}

时间: 2024-10-24 03:17:26

(简单) POJ 1847 Tram,Dijkstra。的相关文章

POJ 1847 Tram 单源最短路径

题意:轨道网,有若干转换器,每个转换器都和其他若干转换器相连,转换器初始指向第一个与其相连的转换器.问要到达终点需要最少转换多少次? 思路:可以用dijkstra单源最短路来做,把轨道网看做有向图(因为1第一个指向2,2的第一个不一定指向1),当前转换器处始指向的那个转换器之间的路径权值为0,其他路径权值为1,求一次起点到终点的最短路,结果就是最少转换次数,注意可能没有路径,这时要输出-1 代码: /* poj 1847 264K 0MS */ #include<cstdio> #includ

POJ 1847 Tram 【最短路,spfa算法,题意理解是关键呀!!】

Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13468   Accepted: 4954 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 t

poj 1847 Tram【spfa最短路】

Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12005   Accepted: 4365 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 t

Floyd_Warshall POJ 1847 Tram

题目传送门 题意:这题题目难懂.问题是A到B最少要转换几次城市.告诉每个城市相连的关系图,默认与第一个之间相连,就是不用转换,其余都要转换. 分析:把第一个城市权值设为0, 其余设为0.然后Floyd跑一下,得到A到B最少转换几次.有点水 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 1e2 + 5; const int INF =

POJ 1847 Tram【Floyd】

题意:给出n个站点,每个站点都有铁路通向其他站点 如果当前要走得路恰好是该站点的开关指向的铁路,则不用扳开关,否则要手动扳动开关,给出起点和终点,问最少需要扳动多少次开关 输入的第一行是n,start,end 接下来的n行,每一行中,第一个数是该站点向外连接的铁路条数, 第二个数是该站点的开关指向的铁路(因为这儿没有读懂= =所以都建不出图来--5555参见这一句话:Switch in the i-th intersection is initially pointing in the dire

poj 1847 最短路简单题,dijkstra

1.poj  1847  Tram   最短路 2.总结:用dijkstra做的,算出a到其它各个点要改向的次数.其它应该也可以. 题意: 有点难懂.n个结点,每个点可通向ki个相邻点,默认指向第一个相邻点,可以改变指向.求一条从A到B的路,使用最少改变路上点的指向的次数. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm>

(简单) POJ 3087 Shuffle&#39;m Up,枚举。

Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several

(简单) POJ 2492 A Bug&#39;s Life,二分染色。

Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs

POJ 3268 双向Dijkstra

Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13020 Accepted: 5832 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N)