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
  • 相关阅读:
    远程视频监控之应用篇(mjpg-streamer)
    C语言存储类型
    基于Qt的wifi智能小车的制作(一)
    libevent和基于libevent的网络编程
    fdisk 分区格式化为ext4格式分区
    Makefile 中:= ?= += =的区别
    【计算机网络】详解网络层(二)ARP和RARP
    Nginx 反向代理 负载均衡 虚拟主机配置
    C# 对文本文件的几种读写方法总结
    Visual Studio中Image Watch的使用
  • 原文地址:https://www.cnblogs.com/whywhy/p/4338488.html
Copyright © 2011-2022 走看看