zoukankan      html  css  js  c++  java
  • HDU 3665 Seaside

    题目链接:

    http://acm.split.hdu.edu.cn/showproblem.php?pid=3665

    Problem Description
    XiaoY is living in a big city, there are N towns in it and some towns near the sea. All these towns are numbered from 0 to N-1 and XiaoY lives in the town numbered ’0’. There are some directed roads connecting them. It is guaranteed that you can reach any town from the town numbered ’0’, but not all towns connect to each other by roads directly, and there is no ring in this city. One day, XiaoY want to go to the seaside, he asks you to help him find out the shortest way.
     
    Input
    There are several test cases. In each cases the first line contains an integer N (0<=N<=10), indicating the number of the towns. Then followed N blocks of data, in block-i there are two integers, Mi (0<=Mi<=N-1) and Pi, then Mi lines followed. Mi means there are Mi roads beginning with the i-th town. Pi indicates whether the i-th town is near to the sea, Pi=0 means No, Pi=1 means Yes. In next Mi lines, each line contains two integers SMi and LMi, which means that the distance between the i-th town and the SMi town is LMi.
     
    Output
    Each case takes one line, print the shortest length that XiaoY reach seaside.
     
    Sample Input
    5
    1 0
    1 1
    2 0
    2 3
    3 1
    1 1
    4 100
    0 1
    0 1
     
    Sample Output
    2
     
    Hint:
    题意:
    一个含有N个小城镇的大城镇,以0到N-1的顺序,先给出两个数a,b;表示i到其他城镇有a条道路,b表示该地方是否有海,0表示没有海,1表示有海。
    然后在给出a1,x;a1表示i到a1之间有道路,距离是x。求从0到有海的城市的最小的距离。
    题解:
    简单的最短路问题。
    代码:
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int maxn = 10+10;
    #define met(a,b) memset(a,b,sizeof(a))
    #define inf 0x3f3f3f3f
    int map[maxn][maxn];
    int visited[maxn],dis[maxn];
    int num[maxn];
    int n,m;
    void dijkstra(int x)
    {
        int min=0,p=0;
        for(int i=0;i<n;i++)
        {
            dis[i]=map[x][i];
            visited[i]=0;
        }
        visited[x]=1;
        for(int i=0;i<n;i++)
        {
            min=inf;
            for(int j=0;j<n;j++)
            {
                if(!visited[j]&&dis[j]<min)
                    {
                        min=dis[j];
                        p=j;
                    }
            }
            visited[p]=1;
            for(int j=0;j<n;j++)
            {
                if(!visited[j]&&dis[p]+map[p][j]<dis[j])
                    dis[j]=dis[p]+map[p][j];
            }
        }
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                map[i][j]=inf;
            met(num,0);
            for(int i=0;i<n;i++)
            {
                scanf("%d%d",&m,&num[i]);
                for(int j=0;j<m;j++)
                {
                    int x,y;
                    scanf("%d%d",&x,&y);
                    map[i][x]=map[x][i]=y;
                }
            }
            dijkstra(0);
            int ans=inf;
            for(int i=0;i<n;i++)
            {
                if(num[i]==1)
                    ans=min(ans,dis[i]);
            }
            printf("%d
    ",ans);
        }
    }
    

      

     
  • 相关阅读:
    SSH免密码登录
    Qt编译错误GL/gl.h: No such file or directory
    UVA 11645
    《逆袭大学》文摘——9.4 基础和应用的平衡中找到大学的节奏
    EBS採购模块中的高速接收和高速接收事务
    笔记-Android中打开各种格式的文件(apk、word、excel、ppt、pdf、音视频、图片等)
    git 冲突解决的方法
    SICP 习题 (1.43)解题总结
    Swift百万线程攻破单例(Singleton)模式
    setjmp/longjmp
  • 原文地址:https://www.cnblogs.com/TAT1122/p/5853273.html
Copyright © 2011-2022 走看看