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
  • 相关阅读:
    计算机图形学——几何变换的数学基础
    算法设计与分析——多边形游戏(动态规划)
    算法设计与分析——凸多边形最优三角剖分(动态规划)
    计算机图形学——反走样
    Android 5.0 API
    Android 6.0 API
    Android 7.0 新增功能和api
    Android 8.0 功能和 API
    Android P 功能和 API
    解决华为手机无法输出Debug级别log的问题
  • 原文地址:https://www.cnblogs.com/whywhy/p/4338488.html
Copyright © 2011-2022 走看看