• HDU-3665 Seaside


    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 S Mi and L Mi, which means that the distance between the i-th town and the S Mi town is L Mi.
    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
    最短路径,用迪杰斯特拉或者SPFA;

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    
    using namespace std;
    const int N = 10 + 5;
    const int INF = (1<<28);
    int mat[N][N],D[N];
    bool visit[N],near_seaside[N];
    
    void Init(){
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++) mat[i][j] = INF;
            D[i] = INF,visit[i] = near_seaside[i] = false;
        }
    }
    int solve(const int n){
        int cnt,i,k;
        D[0] = 0;
        for(cnt = 1;cnt<=n;cnt++){
            for(k=-1,i=0;i<n;i++)
                if(!visit[i]&&(k==-1||D[i] <D[k])) k = i;
            for(visit[k]=true,i=0;i<n;i++)
                if(!visit[i]&&D[i] > D[k]+mat[k][i])
                D[i] = D[k] + mat[k][i];
        }
        int ans = INF;
        for(int i=0;i<n;i++)
            if(near_seaside[i]) ans = min(ans,D[i]);
        return ans;
    }
    
    void Input_data(const int n){
        int u,v,c;
        for(int i=0;i<n;i++){
            scanf("%d %d",&u,&c);
            if(c) near_seaside[i] = true;
            for(int j=0;j<u;j++){
                scanf("%d %d",&v,&c);
                if(c < mat[i][v]) mat[i][v] = mat[v][i] = c;
            }
        }
    }
    int main(){
        int n;
        while(scanf("%d",&n)==1){
            Init();
            Input_data(n);
            printf("%d
    ",solve(n));
        }
    }
    
  • 相关阅读:
    modal 过程中添加动画
    SVSegmentedControl 标签页
    带输入框的UIAlertView
    Django小结
    译Step-by-Step Guide on Configuring Django-Userena
    使用国内镜像通过pip安装python 包
    pycryto实现AES加密解密算法
    notepad++搜索的一些东西
    [转]notepad++正则表达式替换字符串详解
    [转]Notepad++快捷键
  • 原文地址:https://www.cnblogs.com/Pretty9/p/7347676.html
Copyright © 2020-2023  润新知