哈理工练习赛 UVALive 5093 F - Seaside(最短路)

F - Seaside
Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu

Description

Download as PDF

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$ \le$N$ \le$10), indicating the number of the towns. Then followed N blocks of data, in block-i there are two integers, Mi(0$ \le$Mi$ \le$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

题意:
 给出一个无向图,多出的仅仅是某个结点是否临海,让我们判断从起点0开始,到最近的临海点距离。
思路:
完全可以在输入的时候记录所有临海的点,dij一遍后,在这些点中找出最小距离值即可。
AC :
/*=============================================================================
#
#      Author: liangshu - cbam 
#
#      QQ : 756029571 
#
#      School : 哈尔滨理工大学 
#
#      Last modified: 2015-09-06 14:39
#
#     Filename: hdu1015.cpp
#
#     Description: 
#        The people who are crazy enough to think they can change the world, are the ones who do ! 
=============================================================================*/
#
    #include<iostream>
    #include<sstream>
    #include<algorithm>
    #include<cstdio>
    #include<string.h>
    #include<cctype>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    using namespace std;
    const int maxn=30003;
    const int INF=0x1f1f1f1f;
    int n,m;
    //标记起始位置
    string s;
    string e;
     set<int >xx;
    struct Edge
    {
        int from,to,dist;
        Edge(int u,int v,int d):from(u),to(v),dist(d) {}
    };
    vector<Edge>edges;//存储边的结点信息
    vector<int>G[maxn];//邻接表
    bool done[maxn];   //标记是否访问过
    int d[maxn];    //距离数组
    struct heapnode //用于优先队列自定义
    {
        int d,u;
        bool operator <(const heapnode rhs) const
        {
            return d > rhs.d;
        }
        heapnode(int dd,int uu):d(dd),u(uu) {}
    };


    void init()//初始化必不可少
    {
        for(int i=0; i<n; i++)
            G[i].clear();
        edges.clear();
    }

    void addedge(int from,int to,int dist)
    {
        edges.push_back(Edge(from,to,dist));
        int m = edges.size();
        G[from].push_back(m-1);
    }

    void dij( int s)
    {
        priority_queue<heapnode>Q;
        for(int i=0; i<=n; i++)//初始化距离数组
            d[i]=INF;
        d[s]=0;
        memset(done ,0,sizeof(done));
        Q.push( heapnode(0,s) );
        while(!Q.empty())
        {
            heapnode x = Q.top();
            Q.pop();
            int u=x.u;
            if(done[u])continue;
            done[u] = true;
            for(int i=0; i<G[u].size(); i++)
            {
                Edge& e=edges[G[u][i]];//取出来一条邻接边
                //cout<<"u = "<<u<<" e.u = "<<e.to<<endl;
                if(d[e.to]>d[u]+e.dist)
                {
                    d[e.to] = d[u] + e.dist;
                    Q.push((heapnode(d[e.to],e.to)));
                }
            }
        }
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            int t=1;
            int ans=0;
            init();
           xx.clear();int a, b;
            for(int i=0; i<n  ; i++)
            {

                scanf("%d%d",&a,&b);
                if(b == 1){
                    xx.insert(i);
                }
                if(a == 0)continue;
                for(int z = 0; z< a; z++){
                    int x, y;
                    scanf("%d%d",&x, &y);
                     addedge(i, x,y);
                addedge(x,i ,y);
                }
            }
            dij(0);
            int M = 0x1f1f1f1f;
            for(int i = 0; i < n  ;i++){
                    if(xx.count(i))
              {
                    M = min(M, d[i]);
              }
            }
            cout<<M<<endl;
            s.clear();
            e.clear();

        }
        return 0;
    }
/*
5
1 0
1 1
2 0
2 3
3 5
1 1
4 100
0 1
0 1
    */



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值