hdu 1535 Invitation Cards (最短路径)

Invitation Cards

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1588    Accepted Submission(s): 714


Problem 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
 
Recommend
LL   |   We have carefully selected several similar problems for you:  1317 1531 1546 1384 1245 
 
 1 //1125MS    41076K    1434 B    G++
 2 /*
 3 
 4         其实开始让我做我是拒绝的,不能叫我做我马上做,我得先试一下。不然加了特效搞得好像很容易那样,结果群众却做不出来,
 5     我会被批的。
 6     
 7     题意:
 8         有编号1~P个点,有Q条单向路,求 1到其他P-1个点 + 其他P-1个点到1 的最短路线
 9         
10     最短路径:
11         首先数据比较大,用spfa比较靠谱。其实,第一个很明显以1为源点进行一次spfa就出来了,问题是第二个的求法,
12     首先遍历P-1个点是会TLE的,所以要转一下思维,就是逆向思维。把原来的路线全都反向再建图(即把u->v变成v->u),
13     在来一次源点为1的spfa就得出解了。 
14 
15 */
16 #include<iostream>
17 #include<vector>
18 #include<queue>
19 #define N 1000005
20 #define inf 0x7ffffff
21 using namespace std;
22 struct node{
23     int v,w;
24     node(int a,int b){
25         v=a;w=b;
26     }
27 };
28 vector<node>V[N];
29 int d[N];
30 int vis[N];
31 int p,q;
32 int a[N],b[N],w[N]; //记录路线信息 
33 void spfa(int s)  //spfa模板 
34 {
35     for(int i=0;i<=p;i++) d[i]=inf;
36     memset(vis,0,sizeof(vis));
37     queue<int>Q;
38     Q.push(s);
39     d[s]=0;
40     while(!Q.empty()){
41         int u=Q.front();
42         Q.pop();
43         vis[u]=0;
44         int n0=V[u].size();
45         for(int i=0;i<n0;i++){
46             int v=V[u][i].v;
47             int w=V[u][i].w;
48             if(d[v]>d[u]+w){
49                 d[v]=d[u]+w;
50                 if(!vis[v]){
51                     Q.push(v);
52                     vis[v]=1;
53                 }
54             }
55         }
56     }
57 }
58 int main(void)
59 {
60     int t;
61     scanf("%d",&t);
62     while(t--)
63     {
64         scanf("%d%d",&p,&q);
65         for(int i=0;i<=p;i++) V[i].clear();
66         int ans=0; 
67         for(int i=0;i<q;i++){ //正向建图 
68             scanf("%d%d%d",&a[i],&b[i],&w[i]);
69             V[a[i]].push_back(node(b[i],w[i])); 
70         }
71         spfa(1);
72         for(int i=1;i<=p;i++) ans+=d[i];
73         for(int i=0;i<=p;i++) V[i].clear();
74         for(int i=0;i<q;i++) //反向建图 
75             V[b[i]].push_back(node(a[i],w[i]));
76         spfa(1);
77         for(int i=1;i<=p;i++) ans+=d[i];
78         printf("%d
",ans);
79     }
80     return 0;
81 }
原文地址:https://www.cnblogs.com/GO-NO-1/p/3439799.html