• (简单) 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
  • 相关阅读:
    linux MySQL安装指南
    Ubuntu MySQL常用命令
    linux下 mysql 忘记root用户密码解决办法
    ubuntu mysql 启动命令/配置my.cnf
    Ubuntu中给mysql添加新用户并分配权限
    各种 ADSL Modem 及路由器的端口映射方法
    (转)有关SharePoint页面定制的若干细节
    获取Excel中的Sheet的名称
    获取excel表单中的数据
    如何用Excel 2007同步MOSS列表(转)
  • 原文地址:https://www.cnblogs.com/whywhy/p/4338488.html
Copyright © 2020-2023  润新知