Tram POJ - 1847 spfa

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1100,INF=0x3f3f3f3f;
int h[N],e[N],v[N],w[N],ne[N],idx;
int dist[N];
bool st[N];
int n,beg,en,ans;
void add(int a,int b,int c)
{
    v[idx]=a;
    e[idx]=b;
    w[idx]=c;
    ne[idx]=h[a];
    h[a]=idx++;
}
void spfa(int sx)
{
    queue<int>q;
    memset(dist,0x3f,sizeof dist);
    memset(st,0,sizeof st);
    st[sx]=1;
    q.push(sx);
    dist[sx]=0;
    while(q.size())
    {
        int u=q.front();
        q.pop();
        st[u]=0;
        for(int i=h[u];i!=-1;i=ne[i])
        {
            int j=e[i];
            if(dist[j]>dist[u]+w[i])
            {
                dist[j]=dist[u]+w[i];
                if(!st[j])
                {
                    st[j]=1;
                    q.push(j);
                }
            }
        }
    }
    if(dist[en]==INF)
        cout<<"-1"<<endl;
    else
        cout<<dist[en]<<endl;
}
void init()
{
    ans=0;
    memset(h,-1,sizeof(h));
}
void getmap()
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        int k;
        scanf("%d",&k);
        for(j=0;j<k;j++)
        {
            int a;
            scanf("%d",&a);
            if(j==0)
            add(i,a,0);
            else
            add(i,a,1);
        }
    }
}
int main()
{
    while(~scanf("%d%d%d",&n,&beg,&en))
    {
        init();
        getmap();
        spfa(beg);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12236327.html

时间: 2024-08-07 01:24:41

Tram POJ - 1847 spfa的相关文章

POJ 1847 Tram 单源最短路径

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

poj 1847 最短路简单题,dijkstra

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

HDU 1847(SPFA)

高仿代码: #include <iostream>#include <string.h>#include <queue>#include <vector>#include <utility>#include <cstdio>using namespace std;#define N 205#define M 2005const int inf = 0x3f3f3f3f;int v[M],w[M],next[M],first[N],d[

poj 3259Wormholes (spfa最短路径)

#include<stdio.h> #include<string.h> #include<limits.h> #include<queue> using namespace std; #define N 5505 #define M 55000//注意边和点集的数组大小 struct edge { int to,value,next; }edges[M]; int heads[N],len=0; int addedge(int u,int v,int w)

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

poj 1847( floyd &amp;&amp; spfa )

http://poj.org/problem?id=1847 一个水题,用来熟悉熟悉spfa和floyd的. 题意:有m条的铁路,要从x,到y, 之后分别就是条铁路与其他铁路的交点.第一个输入的为有n个交点.之后第一个输入的点,当前铁路到这个点是不要转向的,也就是权值为0,其余的权值都为1,求从x到y的最短路,如果到不了输出-1 裸的floyd和spfa: 1 #include <stdio.h> 2 #include <string.h> 3 #define inf 0x3f3f

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

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 =