[kuangbin带你飞]专题四 最短路练习 N POJ 1847

来源:互联网 发布:西班牙内战 知乎 编辑:程序博客网 时间:2024/05/22 06:15
题目地址:https://vjudge.net/contest/66569#problem/N

思路:这个破水题……完全是考英语好吗……重点就是下面这句话:Switch in the i-th intersection is initially pointing in the direction of the first intersection listed. 大致意思就是每次输入的第一个数是不占用转弯次数的,也就是这条边边权为0。知道了这个,剩下的就是大水题了。

AC代码:

#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<vector>#define INF 0x3f3f3f3fusing namespace std;const int maxn=105;vector<pair<int,int> >E[maxn];int d[maxn];int n,s,f;int main(){    while(~scanf("%d%d%d",&n,&s,&f))    {     for(int i=1;i<=n;i++)    {        d[i]=INF;        E[i].clear();        int k;        scanf("%d",&k);       for(int j=1;j<=k;j++)        {            int temp;            scanf("%d",&temp);            if(j==1)            E[i].push_back(make_pair(temp,0));            else            E[i].push_back(make_pair(temp,1));        }    }    d[s]=0;    priority_queue<pair<int,int> >q;    q.push(make_pair(-d[s],s));    while(!q.empty())    {        int now=q.top().second;        q.pop();        for(int i=0;i<E[now].size();i++)        {            int v=E[now][i].first;            if(d[v]>d[now]+E[now][i].second)            {                d[v]=d[now]+E[now][i].second;                q.push(make_pair(-d[v],v));            }        }    }    if(d[f]==INF)        printf("-1\n");    else    printf("%d\n",d[f]);    }}


0 0
原创粉丝点击