码迷,mamicode.com
首页 > 其他好文 > 详细

HDU1535——Invitation Cards(最短路径:SPAF算法)

时间:2014-07-22 22:35:15      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   使用   os   

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.
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

题目大意:
    有编号1~P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接到达终点站,是单向的,每条路线有它自己的车费。

    有P个人早上从1出发,他们要到达每一个公交站点, 然后到了晚上再返回点1。 求所有人来回的最小费用之和。

结题思路:

    求两边最短路,第二遍把边反向。求总和就行了。

    PS:这个题数据比较多和大,使用dijskstra算法和SPFA算法会超时,需要优化。具体细节看备注。

Code:

 1 #include<stdio.h>
 2 #include<limits.h>
 3 #include<iostream>
 4 #include<string>
 5 #include<queue>
 6 #define MAXN 1000000
 7 using namespace std;
 8 struct e
 9 {
10     int begin;
11     int end;
12     int dis;
13 } edge1[MAXN+10],edge2[MAXN+10];
14 int dis[MAXN+10],first[MAXN+10];
15 bool vis[MAXN+10];
16 int T,S,D,N,k,M;
17 void SPFA(int begin,struct e edge[])
18 {
19     for (int i=1; i<=N; i++)
20     {
21         dis[i]=INT_MAX;
22         vis[i]=0;
23     }
24     queue <int> Q;
25     Q.push(begin);
26     dis[begin]=0;
27     while (!Q.empty())
28     {
29         begin=Q.front();
30         Q.pop();
31         vis[begin]=0;
32         for (int i=first[begin]; edge[i].begin==begin; i++) //可以只遍历begin==edge[i].begin的edge
33             if (dis[edge[i].end]>dis[begin]+edge[i].dis)
34             {
35                 dis[edge[i].end]=dis[begin]+edge[i].dis;
36                 if (!vis[edge[i].end])
37                 {
38                     Q.push(edge[i].end);
39                     vis[edge[i].end]=1;
40                 }
41             }
42     }
43 }
44 void init(struct e edge[]) //first存各个顶点作为结点时的第一个下标
45 {
46     memset(first,0,sizeof(first));
47     first[edge[1].begin]=1;
48     for (int i=2;i<=M;i++)
49         if (edge[i-1].begin!=edge[i].begin) first[edge[i].begin]=i;
50 }
51 bool cmp(struct e a,struct e b)
52 {
53     return a.begin<b.begin;
54 }
55 int main()
56 {
57     int T;
58     cin>>T;
59     while (T--)
60     {
61         scanf("%d %d",&N,&M);
62         int x1,x2,x3;
63         for (int i=1; i<=M; i++)
64         {
65             scanf("%d %d %d",&x1,&x2,&x3); //cin跑了2600ms scanf只要1300ms
66             //cin>>x1>>x2>>x3;
67             edge1[i].begin=x1,edge1[i].end=x2,edge1[i].dis=x3;
68             edge2[i].begin=x2,edge2[i].end=x1,edge2[i].dis=x3;
69         }
70         sort(edge1+1,edge1+M+1,cmp); //按begin顶点排序
71         sort(edge2+1,edge2+M+1,cmp);
72         init(edge1);
73         SPFA(1,edge1);
74         int cnt=0;
75         for (int i=1; i<=N; i++)
76             cnt+=dis[i];
77         init(edge2);
78         SPFA(1,edge2);
79         for (int i=1; i<=N; i++)
80             cnt+=dis[i];
81         printf("%d\n",cnt);
82     }
83     return 0;
84 }

HDU1535——Invitation Cards(最短路径:SPAF算法),布布扣,bubuko.com

HDU1535——Invitation Cards(最短路径:SPAF算法)

标签:des   style   blog   color   使用   os   

原文地址:http://www.cnblogs.com/Enumz/p/3860916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!