Poj 1511 invitation cards

Source: Internet
Author: User

Source: http://poj.org/problem? Id = 1511

It takes a long time to understand the question. The goal is to find the sum of the two minimum paths (that is, the travel expenses of this question,

For the first time, the path from CCS (1) to each point is the smallest. The spfa algorithm does not have to say that when it comes back, the end point is determined by CCS (1), which is equivalent to putting the path

In turn, the digraph goes in the inverse direction, and the path from 1 to each point is the smallest, and then a spfa is used. Note that ANS must use long.

Otherwise, it would also be wa, Where wa was made several times. Even though the AC was changed, I still don't understand it. The question clearly says smaller than 1000000000,

It's speechless.

 1 #include<stdio.h> 2 #include<string.h> 3 #define INF 0x3f3f3f3f 4 const int maxn=1000000+10; 5 int u[maxn],v[maxn],w[maxn],next[maxn],first[maxn],dist[maxn],que[maxn]; 6 bool inq[maxn]; 7 void add_edge(int a,int e) 8 { 9     next[e]=first[a];10     first[a]=e;11 }12 void spfa(void)13 {14     int head,tail;15     dist[1]=0;16     que[head=tail=0]=1;17     tail++;18     inq[1]=true;19     while(head!=tail){20         int a=que[head];21         head=(head+1)%maxn;22         inq[a]=false;23         for(int e=first[a];e!=-1;e=next[e]){24             if(dist[v[e]]>dist[a]+w[e]){25                 dist[v[e]]=dist[a]+w[e];26                 if(!inq[v[e]]){27                     inq[v[e]]=true;28                     que[tail]=v[e];29                     tail=(tail+1)%maxn;30                 }31             }32         }33     }34 }35 int main()36 {37     int t,q,p;38     scanf("%d",&t);39     while(t--){40         scanf("%d%d",&p,&q);41         memset(first,-1,sizeof(int)*(p+1));42         for(int e=1;e<=q;e++){43             scanf("%d%d%d",&u[e],&v[e],&w[e]);44             add_edge(u[e],e);45         }46         memset(dist,0x3f,sizeof(int)*(p+1));47         memset(inq,false,sizeof(bool)*(p+1));48         spfa();49         long long ans=0;50         for(int i=1;i<=p;i++)51             ans+=dist[i];52         memset(first,-1,sizeof(int)*(p+1));53         for(int e=1;e<=q;e++){54             int t=u[e];55             u[e]=v[e];56             v[e]=t;57             add_edge(u[e],e);58         }59         memset(dist,0x3f,sizeof(int)*(p+1));60         memset(inq,false,sizeof(bool)*(p+1));61         spfa();62         for(int i=1;i<=p;i++)63             ans+=dist[i];64         printf("%lld\n",ans);65     }66     return 0;67 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.