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


            稀疏圖的多源最短路徑問題,用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 閱讀(672) 評論(2)  編輯 收藏 引用 所屬分類: AlgorithmUSACO 、圖論

            評論

            # re: USACO 3.2 Sweet Butter[未登錄] 2009-08-17 00:10 intheway

            很欣賞你寫的代碼~  回復  更多評論   

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

            .。是不是dijkstra寫錯了啊。。。我是說這個單詞  回復  更多評論   

            導航

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

            統計

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            久久久久久久久久久免费精品| 狠狠色丁香婷婷久久综合五月| 亚洲国产精品久久| 欧美亚洲国产精品久久蜜芽| 久久精品人人做人人爽电影蜜月 | 伊人久久大香线蕉无码麻豆| 久久九九久精品国产| 日韩精品久久无码人妻中文字幕 | 久久久女人与动物群交毛片| 久久久久亚洲AV成人网人人网站| 色欲久久久天天天综合网精品 | 久久久久一级精品亚洲国产成人综合AV区| 99久久婷婷国产综合亚洲| 久久精品国产亚洲AV不卡| 精品综合久久久久久88小说| 久久婷婷色综合一区二区| 亚洲伊人久久综合中文成人网| 91精品国产乱码久久久久久| 狠狠综合久久AV一区二区三区| 国产精品午夜久久| 久久国产精品免费一区二区三区| 97精品久久天干天天天按摩| 色综合久久久久久久久五月| 精品久久久噜噜噜久久久| 996久久国产精品线观看| 热久久视久久精品18| 久久久无码精品亚洲日韩按摩| AV狠狠色丁香婷婷综合久久 | 99久久这里只精品国产免费| 狠狠色丁香久久婷婷综合五月| 中文精品99久久国产| 久久久久久精品免费免费自慰| 精品久久久久国产免费| 亚洲国产成人久久精品99| 国产成人久久精品二区三区| 国产亚洲欧美成人久久片| 久久综合噜噜激激的五月天| 久久天堂AV综合合色蜜桃网| 天堂久久天堂AV色综合| 色综合久久中文字幕无码| 综合久久国产九一剧情麻豆|