[POJ] 1511 Invitation Cards

Invitation Cards
Time Limit: 8000MS   Memory Limit: 262144K
Total Submissions: 18198   Accepted: 5969

Description

In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery. 

The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where 'X' denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan. 

All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees. 

Input

The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.

Output

For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.

Sample Input

2

2 2

1 2 13

2 1 33

4 6

1 2 10

2 1 60

1 3 20

3 4 10

2 4 5

4 1 50

Sample Output

46

210

Source

 
 
 
题解:由题目可知,spfa即可解决。根据spfa的特点,从源点1作spfa以后,各点的dist都是最短路径长,求和。然后求其他n-1个点到1的最短路。根据数据规模,如果作n-1次spfa肯定难以承受,单向道路反向建图,再从源点1作spfa,各点dist再求和,与原图n-1个点到1的dist之和等价。两次和相加即为所求。
       要注意和sum的范围。int sum的话结果会越界导致WA。
 
代码:
  1 #include<stdio.h>

  2 #include<string.h>

  3 #include<stdbool.h>

  4 #include<limits.h>

  5 int i,j,n,m,p,tot,

  6     q[2100000],toit[1100000],list[1100000],next[1100000],cost[1100000],dist[1100000],

  7     ai[1100000],bi[1100000],ci[1100000];

  8 double sum;

  9 bool can[1100000];

 10 

 11 int 

 12 pre(void)

 13 {

 14     memset(q,0,sizeof(q));

 15     memset(toit,0,sizeof(toit));

 16     memset(next,0,sizeof(next));

 17     memset(list,0,sizeof(list));

 18     memset(cost,0,sizeof(cost));

 19     memset(can,true,sizeof(can));

 20     memset(ai,0,sizeof(ai));

 21     memset(bi,0,sizeof(bi));

 22     memset(ci,0,sizeof(ci));

 23     sum=0;tot=0;

 24     return 0;

 25 }

 26 

 27 int 

 28 add(int x,int y,int z)

 29 {

 30     tot++;

 31     cost[tot]=z;

 32     next[tot]=list[x];

 33     list[x]=tot;

 34     toit[tot]=y;

 35     

 36     ai[tot]=y; bi[tot]=x; ci[tot]=z;

 37     return 0;

 38 }

 39 

 40 void 

 41 spfa(int s)

 42 {

 43     int i,j,head,tail,v,k;

 44     q[1]=s;

 45     head=0;tail=1;

 46     for(i=1;i<=n;i++)

 47     dist[i]=INT_MAX >> 1;

 48     dist[s]=0;

 49     can[s]=false;

 50     

 51     while(head!=tail)

 52     {

 53         head=head%2000000+1;

 54         v=q[head];

 55         k=list[v];

 56         

 57         while(k!=0)

 58         {

 59             if((dist[v]+cost[k])<dist[toit[k]])

 60             {

 61               dist[toit[k]]=dist[v]+cost[k];

 62               if (can[toit[k]])

 63               {

 64                 tail=tail%2000000+1;

 65                 q[tail]=toit[k];

 66                 can[toit[k]]=false;

 67               }

 68                

 69             }

 70               k=next[k];

 71         }

 72         can[v]=true;

 73     }

 74     

 75 }

 76     

 77 

 78 int

 79 main()

 80 {

 81     int x,y,z,ca;

 82     scanf("%d\n",&p);

 83 for(ca=1;ca<=p;ca++)

 84 {

 85     pre();

 86     scanf("%d%d\n",&n,&m);

 87     for(i=1;i<=m;i++)

 88     { 

 89         scanf("%d%d%d",&x,&y,&z);

 90         add(x,y,z);

 91     }

 92     spfa(1);

 93     

 94     for(i=1;i<=n;i++)

 95     sum+=dist[i];

 96     

 97     memset(q,0,sizeof(q));

 98     memset(toit,0,sizeof(toit));

 99     memset(next,0,sizeof(next));

100     memset(list,0,sizeof(list));

101     memset(cost,0,sizeof(cost));

102     memset(can,true,sizeof(can));

103     tot=0;

104     

105     for(i=1;i<=m;i++)

106     add(ai[i],bi[i],ci[i]);

107     

108     spfa(1);

109     for(i=1;i<=n;i++)

110     sum+=dist[i];

111  

112     printf("%.f\n",sum);

113 }

114     return 0;

115 }   

116     

117     

118     

119     

 

 

你可能感兴趣的:(poj)