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
  • 相关阅读:
    IT之光
    个人作业3——个人总结(Alpha阶段)
    结对编程2——单元测试
    个人作业2
    结对作业1
    个人作业1
    个人作业3——个人总结(Alpha阶段)
    结对编程2--单元测试
    个人作业2——英语学习APP案例分析
    结对编程1
  • 原文地址:https://www.cnblogs.com/whywhy/p/4338488.html
Copyright © 2011-2022 走看看