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\n",ans);
    }
}

  

时间: 2024-08-29 11:08:09

HDU 3665 Seaside的相关文章

hdu 3665 Seaside floyd+超级汇点

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3665 题意分析:以0为起点,求到Sea的最短路径. 所以可以N为超级汇点,使用floyd求0到N的最短路径. /*Seaside Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1151 Accepted Submission(s): 839 P

HDU 3665 Seaside (最短路,Floyd)

题意:给定一个图,你家在0,让你找出到沿海的最短路径. 析:由于这个题最多才10个点,那么就可以用Floyd算法,然后再搜一下哪一个是最短的. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <io

Seaside HDU 3665 【Dijkstra】

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 guarante

hdu 3665

半个月的期末..然后CZL和CTL神犇就刷了几百道题orz!!!! 区间K大值,主席树入门 这个数据结构就是先离散化后对[1-i]建树,每次多建个相关的链(好神奇的做法) 然后这样就要记录儿子了,所以数组写起来好难看所以就用了指针 1 //#include<bits/stdc++.h> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6

HDU 4430 &amp; ZOJ 3665 Yukari&#39;s Birthday(二分+枚举)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4430 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4888 Problem Description Today is Yukari's n-th birthday. Ran and Chen hold a celebration party for her. Now comes the most import

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

[hdu 2102]bfs+注意INF

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 感觉这个题非常水,结果一直WA,最后发现居然是0x3f3f3f3f不够大导致的--把INF改成INF+INF就过了. #include<bits/stdc++.h> using namespace std; bool vis[2][15][15]; char s[2][15][15]; const int INF=0x3f3f3f3f; const int fx[]={0,0,1,-1};