POJ 1364 King 差分約束 SPFA

來源:互聯網
上載者:User

終於用SPFA AC了~~這題還真是!!稍後要把差分約束和SPFA總結一下~~,覺得這題還是挺有意思的。具體為什麼WA了呢??? 不是那麼清楚~~ 照著做就是AC了。發現形成鏈表就那麼簡單~ 嘿嘿~

#include<iostream>#include<queue>#define MAXN 103#define INF 0x7F7F7F7Fusing namespace std;struct Node{       int v,price;       Node *next;}Edge[MAXN],*ptr[MAXN];int N,M;int edgeNum;void addEdge( int u,int v,int val ){     Node *p=&Edge[edgeNum++];          p->v=v;     p->price=val;     p->next=ptr[u];     ptr[u]=p;}bool spfa(){     bool used[MAXN];     int dist[MAXN],cnt[MAXN];     queue<int>myQueue;     memset( used,true,sizeof(used) );     memset( dist,0,sizeof(dist) );     memset( cnt,0,sizeof(cnt) );          for( int i=0;i<=N;i++ )           myQueue.push(i);          while( !myQueue.empty() )     {            int u=myQueue.front();myQueue.pop();            used[u]=false;            Node *p=ptr[u];            while( p )            {                   if( dist[p->v]>dist[u]+p->price )                   {                       dist[p->v]=dist[u]+p->price;                       if( !used[p->v] )                       {                           myQueue.push(p->v);                           used[p->v]=true;                           if( ++cnt[p->v]>N )                               return false;                       }                   }                   p=p->next;            }     }     return true;}int main(){    while( scanf( "%d",&N )!=EOF )    {           edgeNum=0;           if( N==0 )               break;           scanf( "%d",&M );           int i,j,k;           char com[3];           int a,b,p;           for( i=0;i<=N;i++ ) ptr[i]=NULL;                      for( i=1;i<=M;i++ )           {                scanf( "%d %d %s %d",&a,&b,&com,&p );                if( com[0]=='g' )                    addEdge( a+b,a-1,-p-1 );                else                    addEdge( a-1,a+b,p-1 );           }           if( !spfa() )               printf( "successful conspiracy\n" );           else               printf( "lamentable kingdom\n" );     }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.