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

来源:互联网 发布:增加力量 知乎 编辑:程序博客网 时间:2024/06/06 01:14
F - Seaside
Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu
SubmitStatusPracticeUVALive 5093

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 toN - 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 integerN(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 areMi roads beginning with the i-th town. Pi indicates whether thei-th town is near to the sea, Pi = 0 means `No', Pi = 1 means `Yes'. In nextMi lines, each line contains two integersSMi and LMi, which means that the distance between thei-th town and the SMi town isLMi.

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;    }/*51 01 12 02 33 51 14 1000 10 1    */


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 在工作中把和别人结下梁子了怎么办 在阿里巴巴上买的货物没发货怎么办 做了下颌角一边脸反复肿怎么办 玩广东11选5输50万怎么办 玩广东11选5输了十几万怎么办 新办公楼装修好就要搬进去怎么办 oppo手机上的黄页删了怎么办 小米不小心把手机联系人删了怎么办 58同城小区名输不了怎么办 e路发注册直接有积分怎么办 大众速腾的不锈钢圈被碰花了怎么办 公司卡着生育险不给怎么办 济南图书馆借书超过期限了怎么办 淘宝解绑支付宝支付密码不对怎么办 微博支付宝支付密码忘记了怎么办 图虫签约师通过了认证标识怎么办 签证做假工作证明资料被拒怎么办 在广州办个建设厅电工证怎么办 水利考的五大员证到有效期怎么办 额头注射玻尿酸吸收后不平怎么办 施工员证书挂靠拿不回来怎么办 森林公安未转政法编制的人员怎么办 北京的限行新政策外地车怎么办 报考二级建造师工作年限不够怎么办 郑州航院图书馆密码忘了怎么办 无锡妇幼预约挂号过号了怎么办 云南建筑八大员考试没通过怎么办 订车ax7一个月提不到车怎么办 提车时间到了却没车怎么办 一汽大众速腾气囊灯亮该怎么办呢? 幼儿园上课时候电脑上的课件怎么办 黑米紫薯红豆粥不好煮怎么办 母狗生了小狗后不吃东西没奶怎么办 狗给扑倒了主人不想负责怎么办 山东政务网个人中心账号忘了怎么办 网易博客忘记登入名和密码了怎么办 奶水不够怎么办怎样让奶水变多 和初恋分手多年又爱上初恋该怎么办 分手六年的初恋想要和我复合怎么办 不小心把手机里的视频删了怎么办 网签过了中介不配合过户怎么办