zoukankan      html  css  js  c++  java
  • (简单) 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
    ",ans[B]==INF ? -1 : ans[B]);
    
        return 0;
    }
    View Code
  • 相关阅读:
    面试6 在c#中如何声明一个类不能被继承
    面试5 如何理解静态变量,局部变量,全局变量
    面试4 你在什么情况下会用到虚方法?它与接口有什么不同
    面试3 不用系统自带的方法将字符串类型123456转换为值类型
    面试2 递归的算法求1,1,2,3,5,8.......的第30位数是多少,然后求这些数的和.
    面试1 SQL SERVER 查询第20行到30之间的数据
    ubuntu安装nginx
    ubuntu上使用ufw配置管理防火墙
    ubuntu上安装docker
    ubuntu上使用vim编辑文本内容
  • 原文地址:https://www.cnblogs.com/whywhy/p/4338488.html
Copyright © 2011-2022 走看看