zoukankan      html  css  js  c++  java
  • [POJ1847] Tram

    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
    

    Source

    Croatia OI 2002 Regional - Juniors

    题解

    关键在于读懂题目,再就是简单的(Dijkstra)

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<queue>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<cstdlib>
    #include<cmath>
    #include<map>
    #include<vector>
    #include<set>
    #include<bitset>
    #include<stack>
    using namespace std;
    
    const int N=101,NE=10001;
    int n,From,End;
    struct edge
    {
    	int v,w,nx;
    }e[NE];
    int ne,hd[N];
    
    void Build(int u,int v,int w)
    {
    	++ne,e[ne]=(edge){v,w,hd[u]},hd[u]=ne;
    }
    
    void Read()
    {
    	scanf("%d%d%d",&n,&From,&End);
    	int num,to;
    	for(int i=1;i<=n;++i)
    	{
    		scanf("%d",&num);
    		for(int j=0;j<num;++j)
    		{
    			scanf("%d",&to);
    			if(j) Build(i,to,1);
    			else Build(i,to,0);
    		}
    	}
    }
    
    typedef pair<int,int> pr;
    priority_queue<pr,vector<pr>,greater<pr> >Q;
    bool vis[N];
    int dis[N];
    const int INF=10000000;
    
    void Dijkstra(int s)
    {
    	for(int i=1;i<=n;++i) dis[i]=INF;
    	dis[s]=0;
    	Q.push(make_pair(dis[s],s));
    	int nw,to,YH;
    	while(!Q.empty())
    	{
    		nw=Q.top().second;
    		Q.pop();
    		if(vis[nw]) continue;
    		vis[nw]=1;
    		for(int i=hd[nw];i;i=e[i].nx)
    		{
    			to=e[i].v,YH=dis[nw]+e[i].w;
    			if(dis[to]>YH)
    			{
    				dis[to]=YH;
    				Q.push(make_pair(dis[to],to));
    			}
    		}
    	}
    }
    
    int main()
    {
    	Read(),Dijkstra(From);
    	if(dis[End]<INF) printf("%d
    ",dis[End]);
    	else puts("-1");
    	return 0;
    }
    

    本文作者:OItby @ https://www.cnblogs.com/hihocoder/

    未经允许,请勿转载。

  • 相关阅读:
    C#读写设置修改调整UVC摄像头画面-曝光
    C#读写调整修改设置UVC摄像头画面-逆光对比
    C#读写修改设置调整UVC摄像头画面-清晰度
    C#读写设置修改调整UVC摄像头画面-对比度
    C#读写调整设置UVC摄像头画面-亮度
    C#采集UVC摄像头画面并支持旋转和分辨率切换
    一张图看懂SharpCamera
    C#混音同时录制采集声卡和麦克风话筒
    C#采集麦克风话筒声音
    Windows下安装Redis服务
  • 原文地址:https://www.cnblogs.com/hihocoder/p/11479822.html
Copyright © 2011-2022 走看看