hdu 3665 Seaside【裸最短路】

链接:

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

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=29015#problem/C

Seaside

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 944    Accepted Submission(s): 670


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 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
 

Source
2010 Asia Regional Harbin

题意:

从编号为 0 的城市出发,求到达沿海城市的最短距离

第一行 N 表示城市的个数(编号从 0 开始)

然后剩下 N 堆数据:第 i堆第一行表示编号为 i 的城市有 mi 条路可以到达其他城市,

                                      Pi 表示该城市是否沿海【0不沿海,1沿海】

                                      下面 mi 行的两个数分别表示达到的城市编号和距离。


注意输入顺序,简单处理下就好了

城市编号从 0 开始


算法:Dijkstra

code:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int maxn = 20;
const int INF = 100000000;
int w[20][20];
int d[maxn];
int vis[maxn];
int s[maxn];
int n;

void Dijkstra()
{
    for(int i = 0; i < n; i++) d[i] = w[0][i];
    d[0] = 0;

    memset(vis, 0, sizeof(vis));

    for(int i = 1; i <= n; i++)
    {
        int m = INF;
        int x;
        for(int y = 0; y < n; y++) if(!vis[y] && d[y] <= m)
        m = d[x=y];
        vis[x] = 1;
        for(int y = 0; y < n; y++)
            d[y] = min(d[y], d[x]+w[x][y]);
    }

    int ans = INF;
    for(int i = 0; i < n; i++)
    {
        if(s[i] && d[i] < ans) //找出沿海的最短距离
            ans = d[i];
    }
    printf("%d\n", ans);
}

int main()
{
    while(scanf("%d", &n) != EOF)
    {
        for(int i = 0; i <= n; i++)
            for(int j = 0; j <= n; j++)
                w[i][j] = (i == j ? 0 : INF);

        memset(s, 0, sizeof(s));
        for(int i = 0; i < n; i++)
        {
            int mi,pi;
            scanf("%d%d", &mi, &s[i]);
            int si,li;
            while(mi--)
            {
                scanf("%d%d", &si,&li);
                w[i][si] = min(w[i][si], li);
                w[si][i] = w[i][si];
            }
        }
        Dijkstra();
    }
    return 0;
}



你可能感兴趣的:(hdu 3665 Seaside【裸最短路】)