• 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);
        }
    }
    

      

     
  • 相关阅读:
    操作系统
    MarkDown语法实操
    mac 添加环境变量
    在django中建立mysql数据表时发生的低级错误
    models.Book.object.get()与models.Book.object.filter()区别
    django中数据库操作时distinct的用法
    ‘,’逗号并不是字符串的拼接方式
    django单表操作中update_or_create不能更新多于一个数据的信息
    在python中terminal中建立mysql数据库,无法再models.py 文件中建立数据库信息
    booleanfield()和booleanfield(default=True)在数据库的表中无法插入
  • 原文地址:https://www.cnblogs.com/TAT1122/p/5853273.html
Copyright © 2020-2023  润新知