poj 1511--Invitation Cards
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.
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
题意很简单,首先是求从起点出发到各个站点的最短路径,这就用一般的求单源最短路的方法求就行了,由于n的范围的特殊,所以考虑到用spfa算法
另,题目中还让求它回来时的最短路劲,我原本想的是从最后一点出发往前推得,可是无法确定从哪儿出发,听兄弟解释可以用反向建边再求一次,以1
为出发点,那么求出来的就是回来的路程。代码中我用了slf优化,可是不知道为嘛比普通的还要慢一些- -
View Code
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <algorithm> 5 using namespace std; 6 const int N =1000010; 7 const int INF=0x7f7f7f7f; 8 #define ll __int64 9 struct eg 10 { 11 int to,next,w; 12 } p[N],q[N]; 13 int head[N],head1[N],vis[N]; 14 ll dis[N],sum1,sum2; 15 int cnt,tot,n,m; 16 void addedge(int u,int v,int w) 17 { 18 p[cnt].to=v; 19 p[cnt].w=w; 20 p[cnt].next=head[u]; 21 head[u]=cnt++; 22 } 23 void faddedge(int u,int v,int w) 24 { 25 q[tot].to=v; 26 q[tot].w=w; 27 q[tot].next=head1[u]; 28 head1[u]=tot++; 29 } 30 void spfa(int first[],eg edge[],ll& sum) 31 { 32 int i; 33 memset(vis,0,sizeof(vis)); 34 memset(dis,INF,sizeof(dis)); 35 dis[1]=0; 36 vis[1]=1; 37 deque<int>myque; 38 myque.push_back(1); 39 while(!myque.empty()) 40 { 41 int u=myque.front(); 42 myque.pop_front(); 43 vis[u]=0; 44 for(i=first[u]; i!=-1; i=edge[i].next) 45 { 46 int v=edge[i].to; 47 if(dis[v]>dis[u]+edge[i].w) 48 { 49 dis[v]=dis[u]+edge[i].w; 50 if(!vis[v]) 51 { 52 vis[v]=1; 53 if(!myque.empty()) 54 { 55 if(dis[v]<dis[myque.front()]) 56 myque.push_back(v); 57 else 58 myque.push_front(v); 59 } 60 else 61 myque.push_back(v); 62 } 63 } 64 } 65 } 66 sum=0; 67 for(i=1; i<=n; i++) 68 sum+=dis[i]; 69 } 70 int main() 71 { 72 int t,u,v,w; 73 scanf("%d",&t); 74 while(t--) 75 { 76 cnt=tot=0; 77 memset(head,-1,sizeof(head)); 78 memset(head1,-1,sizeof(head1)); 79 scanf("%d%d",&n,&m); 80 while(m--) 81 { 82 scanf("%d%d%d",&u,&v,&w); 83 addedge(u,v,w); 84 faddedge(v,u,w); 85 } 86 spfa(head,p,sum1); 87 spfa(head1,q,sum2); 88 printf("%I64d\n",sum1+sum2); 89 } 90 return 0; 91 }