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

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算法)

时间: 2024-10-12 22:31:04

HDU1535——Invitation Cards(最短路径:SPAF算法)的相关文章

poj1511||hdu1535 Invitation Cards spfa

题目链接: Invitation Cards 题意: 给出一个M个点N条边的单向图 1 <= M,N <= 1000000.   给出每条边的两端和经过所需要的时间,求从第一点分别到达所有其他点(2~M)再回到第一点的最短时间 题解: 1 <= M,N <= 1000000.     邻接矩阵肯定会爆内存  所以spfa邻接表解决即可 注意好反向边的建立 代码: #include<iostream> #include<cstdio> #include<

hdu1535——Invitation Cards

Invitation Cards Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2173    Accepted Submission(s): 1056 Problem Description In the age of television, not many people attend theater performances.

POJ 1511 Invitation Cards 【最短路,spfa算法,Dijkstra算法堆优化】

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 25219   Accepted: 8346 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They wan

图论trainning-part-1 E. Invitation Cards

E. Invitation Cards Time Limit: 8000ms Memory Limit: 262144KB 64-bit integer IO format: %lld      Java class name: Main In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. Th

POJ 1511 Invitation Cards

题目来源:http://poj.org/problem?id=1511 题目很长,花了不少时间才理解题意,目的就是为了求出来回两次最小路径(即为本题的差旅费)之和, 第一次从CCS(1)出发到各个点路径最小,SPFA算法没得说,回来时终点是确定的都是CCS(1),相当于把路 径反过来,即把有向图去反方向,又是从1出发到各个点路径最小,再用一个SPFA.注意ans要用long long 不然也WA,这个地方WA了好几次,虽然更改后AC了,但还是不明白,题目明明写了smaller than 1000

POJ1511 Invitation Cards【SPFA】

Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 20229 Accepted: 6612 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to

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

hdu 1535 Invitation Cards(有向图的来回最短路,要反向建图)

题目: 链接:点击打开链接 题意: 给一个图,求1到各点和各点到1最短路. 思路: 先spfa,然后反向建图,在spfa就行了. 代码: #include <iostream> #include <cstdio> #include <queue> #include <cstring> using namespace std; #define INF 100000000 const int N = 1000010; struct node{ int u,v,w

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