• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            USACO 3.2 Sweet Butter


            稀疏圖的多源最短路徑問(wèn)題,用heap+disjktra解決。

            #include?<iostream>
            #include?
            <fstream>
            #include?
            <list>
            #include?
            <queue>

            using?namespace?std;

            ifstream?fin(
            "butter.in");
            ofstream?fout(
            "butter.out");

            #ifdef?_DEBUG
            #define?out?cout
            #define?in?cin
            #else
            #define?out?fout
            #define?in?fin
            #endif

            int?n,p,c;

            struct?edge{
            ????
            int?node;
            ????
            int?weight;
            ????edge(
            int?n,int?w):node(n),weight(w){
            ????}
            };

            struct?graph_node{
            ???list
            <edge>?adj;?
            };

            struct?pri_que_node{
            ????
            int?num;
            ????
            int?dist;
            ????pri_que_node(
            int?n,int?d):num(n),dist(d){
            ????};
            ????
            bool?operator<?(const?pri_que_node?&n2)?const{
            ????????
            return?dist>n2.dist;
            ????}
            };

            graph_node?graph[
            801];
            int?cow_place[801];
            int?shortest_path[801][801];

            void?get_shortest(int?n)
            {
            ????
            for(int?i=1;i<801;++i)
            ????????shortest_path[n][i]?
            =?INT_MAX;

            ????shortest_path[n][n]?=?0;
            ????priority_queue
            <pri_que_node>?pq;
            ????
            for(list<edge>::iterator?li?=?graph[n].adj.begin();
            ???????????????????li
            !=graph[n].adj.end();
            ???????????????????li
            ++){
            ????????pq.push(?pri_que_node(li
            ->node,li->weight)?);
            ????????shortest_path[n][li
            ->node]?=?li->weight;
            ????}

            ????
            while(?!pq.empty()?){
            ???????pri_que_node?node?
            =?pq.top();
            ???????pq.pop();

            ???????
            if(?node.dist>?shortest_path[n][node.num]?)
            ???????????
            continue;

            ???????shortest_path[n][node.num]?
            =?node.dist;

            ???????
            for(list<edge>::iterator?li?=?graph[node.num].adj.begin();
            ???????????????????li
            !=graph[node.num].adj.end();
            ???????????????????li
            ++){
            ???????????
            if(?node.dist+li->weight?<?shortest_path[n][li->node]?){
            ???????????????shortest_path[n][li
            ->node]?=?node.dist+li->weight;
            ???????????????pq.push(?pri_que_node(li
            ->node,?shortest_path[n][li->node])?);
            ???????????}
            ????????}
            ????}
            }

            void?solve()
            {
            ????
            in>>n>>p>>c;

            ????
            for(int?i=0;i<n;++i)
            ????????
            in>>cow_place[i];

            ????
            int?a,b,w;
            ????
            while(c--){
            ????????
            in>>a>>b>>w;
            ????????graph[a].adj.push_back(?edge(b,w)?);
            ????????graph[b].adj.push_back(?edge(a,w)?);
            ????}
            #ifdef?_DEBUG
            ????
            for(int?i=1;i<=p;++i){
            ????????
            if(!graph[i].adj.empty()){
            ????????????cout
            <<"node:"<<i<<"?";
            ???????????
            for(list<edge>::iterator?li?=?graph[i].adj.begin();
            ???????????????????li
            !=graph[i].adj.end();
            ???????????????????li
            ++){
            ???????????????cout
            <<"("<<li->node<<","<<li->weight<<")";
            ???????????}
            ???????????cout
            <<endl;
            ????????}
            ????}
            #endif

            ????
            for(int?i=1;i<=p;++i)
            ????????get_shortest(i);

            ????
            int?res?=?INT_MAX;

            ????
            for(int?i=1;i<=p;++i){
            ????????
            int?t?=?0;
            ????????
            for(int?j=0;j<n;++j){
            ????????????t
            +=?shortest_path[i][cow_place[j]];
            ????????}
            ????????res?
            =?min(res,t);
            ????}

            ????
            out<<res<<endl;

            }

            int?main(int?argc,char?*argv[])
            {
            ????solve();?
            ????
            return?0;
            }



            posted on 2009-07-06 20:05 YZY 閱讀(688) 評(píng)論(2)  編輯 收藏 引用 所屬分類: AlgorithmUSACO圖論

            評(píng)論

            # re: USACO 3.2 Sweet Butter[未登錄](méi) 2009-08-17 00:10 intheway

            很欣賞你寫(xiě)的代碼~  回復(fù)  更多評(píng)論   

            # re: USACO 3.2 Sweet Butter 2010-07-27 10:49 onlydyer

            .。是不是dijkstra寫(xiě)錯(cuò)了啊。。。我是說(shuō)這個(gè)單詞  回復(fù)  更多評(píng)論   

            導(dǎo)航

            <2009年6月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            統(tǒng)計(jì)

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            搜索

            積分與排名

            最新評(píng)論

            閱讀排行榜

            青春久久| 久久国产乱子伦精品免费午夜| 国产成人久久久精品二区三区| 国内精品久久久久影院免费| 久久人妻少妇嫩草AV蜜桃| 久久九色综合九色99伊人| 麻豆国内精品久久久久久| 狠狠色丁香久久婷婷综合蜜芽五月| 手机看片久久高清国产日韩| 久久av免费天堂小草播放| 亚洲国产日韩欧美综合久久| 国产高清国内精品福利99久久| 国产高潮国产高潮久久久91 | 一本一道久久综合狠狠老| 久久夜色精品国产欧美乱| 秋霞久久国产精品电影院| 亚洲精品NV久久久久久久久久 | 亚洲国产成人乱码精品女人久久久不卡 | 嫩草伊人久久精品少妇AV| 品成人欧美大片久久国产欧美... 品成人欧美大片久久国产欧美 | 亚洲精品无码久久一线| 精品久久一区二区三区| 一个色综合久久| 国内精品久久久久久久久电影网| 亚洲另类欧美综合久久图片区| 亚洲精品无码专区久久久 | 国産精品久久久久久久| 嫩草伊人久久精品少妇AV| 一级做a爰片久久毛片看看| 久久精品成人免费看| 精品久久久无码21p发布| 99久久国产主播综合精品| 久久久久亚洲AV无码专区体验| 久久婷婷五月综合色99啪ak| 亚洲中文字幕久久精品无码喷水| 久久精品国产黑森林| 欧美久久综合性欧美| 久久久久久国产精品免费无码| 久久婷婷人人澡人人爽人人爱| 久久夜色精品国产亚洲av| 国产一区二区三精品久久久无广告|