查看: 1458|回復: 2
打印 上一主題 下一主題

[TOJ] 43 - PD. To The Target.

[複製鏈接]
  • TA的每日心情
    慵懶
    2015-4-10 14:18
  • 簽到天數: 78 天

    [LV.6]常住居民II

    176

    主題

    612

    帖子

    3959

    積分

    管理員

    Rank: 9Rank: 9Rank: 9

    積分
    3959

    台南一中資訊社新手達陣程式設計達人 - 2014

    跳轉到指定樓層
    樓主
    發表於 2014-5-26 21:43:52 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式

    趕快加入我們來參與討論吧!

    您需要 登錄 才可以下載或查看,沒有帳號?加入我們

    x
    看大家的CODE,我吐血前趕快發這一篇
    原題:http://toj.twbbs.org/oj/pro/43/
    AC:http://toj.twbbs.org/oj/chal/2260/ (非管理員不可見)

    此題為裸最短路,就是求最短路如此而已。別以為用Warshall可以過,你覺得[tex]10000^3[/tex]可以跑得完再說。
    (更正SPFA 原始版本有誤)
    多種作法附註解

    1.SPFA 32ms
    #include<vector>
  • #include<cstdio>
  • #include<queue>
  • #include<cstring>
  • using namespace std;

  • struct edge{
  •     int e,w;
  • };
  • vector<edge> V[10000];//Graph Data
  • int dist[10000]; //Save the distance from S
  • bool inst[10000];//Flag of if the points have being in stack
  • int N,M,S,E;
  • int main(){
  •     int a,b,w;
  •     while(~scanf("%d%d",&N,&M)){
  •         for(int i=0;i<N;++i)V[i].clear();
  •         memset(dist,0x3f,sizeof(dist)); //init to INF
  •         memset(inst,false,sizeof(inst));//init to false
  •         while(M--){
  •             scanf("%d%d%d",&a,&b,&w);
  •             V[a].push_back((edge){b,w});//Add edge
  •             V[b].push_back((edge){a,w});
  •         }
  •         scanf("%d%d",&S,&E);
  •         //Algorithm:SPFA
  •         dist[S]=0;
  •         queue<int> st;    //push the S into stack and set dist[S]=0
  •         st.push(S);        //point : don't set inst[S]=true; it is foolish!
  •         while(!st.empty()){//loop until stack is empty
  •             int t=st.front();st.pop();
  •             inst[t]=false;
  •             for(edge &e:V[t]){//for C++11
  •                 if(dist[e.e]>dist[t]+e.w){//Relax
  •                     dist[e.e]=dist[t]+e.w;
  •                     if(!inst[e.e]){//if point don't stay in stack, push it in
  •                         st.push(e.e);
  •                         inst[e.e]=true;
  •                     }
  •                 }
  •             }
  •         }
  •         printf("%d\n",dist[E]);
  •     }
  • }




  • 2.BellmanFord 12ms
    #include<vector>
  • #include<cstdio>
  • #include<cstring>
  • #include<queue>
  • #include<iostream>
  • using namespace std;

  • struct edge{
  •     int s,e,w;
  • };
  • vector<edge> e;

  • int dist[10000]; //Save the distance from S
  • int N,M,S,E;
  • int main(){
  •     int a,b,w;
  •     while(~scanf("%d%d",&N,&M)){
  •         memset(dist,0x3f,sizeof(dist)); //init to INF
  •         while(M--){
  •             scanf("%d%d%d",&a,&b,&w);
  •             e.push_back((edge){a,b,w});
  •             e.push_back((edge){b,a,w});
  •         }
  •         scanf("%d%d",&S,&E);
  •         //Algorithm:Bellman-Ford
  •         dist[S]=0;
  •         while(true){
  •             bool update=false;
  •             for(edge &t:e){
  •                 if(dist[t.e]>dist[t.s]+t.w){
  •                     dist[t.e]=dist[t.s]+t.w;
  •                     update=true;
  •                 }
  •             }
  •             if(!update)break;
  •         }
  •         printf("%d\n",dist[E]);
  •     }
  • }




  • 3.Dijkstra with priority queue 28ms
    #include<vector>
  • #include<cstdio>
  • #include<cstring>
  • #include<queue>
  • #include<iostream>
  • using namespace std;

  • struct edge{
  •     int e,w;
  • };
  • inline bool operator<(const edge &a,const edge &b){
  •     return a.w>b.w; //Notice!
  • }
  • vector<edge> V[10000];//Graph Data
  • int dist[10000]; //Save the distance from S
  • int N,M,S,E;
  • int main(){
  •     int a,b,w;
  •     while(~scanf("%d%d",&N,&M)){
  •         for(int i=0;i<N;++i)V[i].clear();
  •         memset(dist,0x3f,sizeof(dist)); //init to INF
  •         while(M--){
  •             scanf("%d%d%d",&a,&b,&w);
  •             V[a].push_back((edge){b,w});//Add edge
  •             V[b].push_back((edge){a,w});
  •         }
  •         scanf("%d%d",&S,&E);
  •         //Algorithm:Dijkstra with Priority Queue
  •         priority_queue<edge> pq;
  •         pq.push((edge){S,0});
  •         while(!pq.empty()){
  •             edge t=pq.top();pq.pop();
  •             if(t.e==E){
  •                 dist[t.e]=t.w;
  •                 break; //Find answer!
  •             }
  •             if(dist[t.e]!=0x3f3f3f3f)continue;
  •             dist[t.e]=t.w;
  •             for(edge &e:V[t.e])
  •                 if(dist[e.e]>dist[t.e]+e.w)
  •                     pq.push((edge){e.e,dist[t.e]+e.w});
  •         }
  •         printf("%d\n",dist[E]);
  •     }
  • }



  • 點評

    jd3
    SPFA強調Faster還比較慢XD  發表於 2014-5-26 21:48
    回復

    使用道具 檢舉

  • TA的每日心情
    開心
    2014-8-14 16:02
  • 簽到天數: 1 天

    [LV.1]初來乍到

    12

    主題

    138

    帖子

    863

    積分

    高級會員

    Rank: 4

    積分
    863

    台南一中資訊社新手達陣

    頭香
    發表於 2014-5-26 22:00:24 來自手機 | 只看該作者
    這也太善良了吧
    回復 支持 反對

    使用道具 檢舉

    您需要登錄後才可以回帖 登入 | 加入我們

    本版積分規則

    快速回覆 返回頂部 返回列表