zoukankan      html  css  js  c++  java
  • 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));
        }
    }
    
  • 相关阅读:
    AIX root用户密码丢失怎么办?
    Oracle 11g Grid Infrastructure 卸载
    Failed to create a peer profile for Oracle Cluster GPnP. gpnptool rc=32512
    管理 IBM AIX 中的用户
    vbox克隆文件的路径如何修改?默认它生成在C盘,怎么修改?
    Oracle_dataguard__11G_配置与维护手册
    AIX管理员常用命令
    如何让你的SQL运行得更快
    ORA00604: 递归 SQL 级别 1 出现错误,ORA01000: 超出打开游标的最大数
    Adobe Acrobat 9.0“ PDFMaker无法找到Adobe PDF Printer 的打印驱动程序”解决办法
  • 原文地址:https://www.cnblogs.com/Pretty9/p/7347676.html
Copyright © 2011-2022 走看看