Luo Gu P1346 tram (requires a little thought Shortest)

Title Description

Has a magical town on a special tram network, which consists of a number of tracks and junctions, each junction are connected to a number of tracks, each track leads to a junction (do not rule out some sightseeing rail revolution after returning possible intersection). In each intersection, there is a decision to switch out of the track, each switch has a default state, after per tram traveling to the junction, only pointed out the switch from the track, if the driver wants to go to another tram track, he must switch the off state.

To travel to the target location, tram drivers often have to switch off, so they would like to ask you to write a program that calculates an intersection from AA A to the intersection BB B requires a minimum of a few switches off switch.

Input Format

First line 33 is three integers N, A, BN, A, B N , A , B ( 2≤N≤100,1≤A, B≤N2 \ Leq N \ Leq 100,. 1 \ Leq A, B \ N Leq 2 N . 1 0 0 , . 1 a , B N), respectively, represents the number of intersections, and tram start, end.

Then there NN N rows, each row has a beginning digital KiK_i K I ( 0≤Ki≤N-10 \ Leq K_i \ Leq. 1 N- 0 K I N - . 1), this intersection represents the KiK_i K I tracks are connected, then there KiK_i K I digital representation for each intersection track is routed, the switch defaults to the track a first digital representation.

Output Format

Only one output file number indicating from AA A to BB minimum number of switches needed to switch B, if not from AA to A BB B, the output -1-1 - 1

Sample input and output

Input # 1 
3 2 1
2 2 3
2 3 1
2 1 2
Output # 1 
0 

after the beginning of this problem has been entangled in a non-designated walking path direction specified direction of the intersection of whether it should be changed. Look after someone else's blog think of the fact that there is no any effect. After assuming that this intersection is equivalent to around a ring again, this time to specify the direction has changed, if you do not press the button, then still continue to go round and round, if you press the button to change to another direction and certainly not the optimal solution; If we do not pass this intersection, then change or not change the specified direction in fact there is no difference, in this case, so to build chart, the set edge weights attached to the specified direction of intersections beginning to 0, the other side is set to 1, then the shortest path to run again.
Note: 1. a number of road junctions may be connected to the 0
   2. If not, then output to -1
#include <bits/stdc++.h>
#define INF 0x3f
using namespace std;
int n,a,b,tot;
const int N=105,M=10005;
int head[N],ver[2*M],Next[2*M],edge[2*M];
bool v[N];
int d[N];
priority_queue<pair<int,int> >q;
void add(int x,int y,int z)
{
    see [ ++ tot] = y;
    edge [tot] = z;
    Next[tot]=head[x];
    head[x]=tot;
}
void Dijkstra()
{
    int i;
    memset(d,0x3f3f3f3f,sizeof(d));
    memset(v,0,sizeof(v));
    d[a]=0;
    q.push(make_pair(0,a));
    while(q.size())
    {
        int x=q.top().second;q.pop();
        if(v[x])continue;
        v[x]=1;
        int i;
        for(i=head[x];i;i=Next[i])
        {
            int y=ver[i];int z=edge[i];
            if(d[y]>d[x]+z)
            {
                d[y]=d[x]+z;
                q.push(make_pair(-d[y],y));
            }
        }    
    }
}
int main ()
{
    cin>>n>>a>>b;
    int i;
    for(i=1;i<=n;i++)
    {
        an int the num;
        cin>>num;
        int j;
        if(num==0)continue;//
        for(j=1;j<=num;j++)
        {
            int temp;
            scanf("%d",&temp);
            if(j==1)
            {
                add(i,temp,0);
            }
            else
            {
                add(i,temp,1);
            }
        }    
    }
    Dijkstra();
    if(d[b]!=0x3f3f3f3f)cout<<d[b];
    else cout<<-1;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/lipoicyclic/p/12334069.html