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));
        }
    }
    
  • 相关阅读:
    js 动态 activex 组件
    nodejs 任务调度使用
    javascript 停止事件冒泡以及阻止默认事件冒泡
    使用SQL字符串反转函数REVERSE巧妙实现lastindexof功能
    morris.js 简单学习
    weblogic启动受管服务器报错Authentication for user weblogic denied (weblogic 11g 域账号密码不生效的解决方法)
    正向代理与反向代理【总结】
    不休息的工作都是浪费时间
    Oracle实例名,服务名等概念区别与联系
    Tomcat启动找不到JRE_HOME的解决方法
  • 原文地址:https://www.cnblogs.com/Pretty9/p/7347676.html
Copyright © 2011-2022 走看看