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

POJ1511 Invitation Cards —— 最短路spfa

时间:2017-09-29 13:22:23      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:nes   ring   vol   img   clu   tor   tin   i++   class   

题目链接:http://poj.org/problem?id=1511

 

Invitation Cards
Time Limit: 8000MS   Memory Limit: 262144K
Total Submissions: 29286   Accepted: 9788

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

 
 
 
题解:
 
 
 
代码如下:
技术分享
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <vector>
  6 #include <cmath>
  7 #include <queue>
  8 #include <stack>
  9 #include <map>
 10 #include <string>
 11 #include <set>
 12 #define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
 13 #define ms(a,b) memset((a),(b),sizeof((a)))
 14 using namespace std;
 15 typedef long long LL;
 16 const double EPS = 1e-8;
 17 const int INF = 2e9;
 18 const LL LNF = 9e18;
 19 const int MOD = 1e9+7;
 20 const int MAXN = 1e6+10;
 21 
 22 int n, m;
 23 
 24 struct edge
 25 {
 26     int from, to, w, next;
 27 }edge[MAXN*4];
 28 int cnt, head[MAXN];
 29 
 30 void add(int u, int v, int w)
 31 {
 32     edge[cnt].from = u;
 33     edge[cnt].to = v;
 34     edge[cnt].w = w;
 35     edge[cnt].next = head[u];
 36     head[u] = cnt++;
 37 }
 38 
 39 void init()
 40 {
 41     cnt = 0;
 42     memset(head, -1, sizeof(head));
 43 }
 44 
 45 LL dis1[MAXN], dis2[MAXN];
 46 int times[MAXN], inq[MAXN];
 47 void spfa(int st, LL dis[])
 48 {
 49     memset(inq, 0, sizeof(inq));
 50     memset(times, 0, sizeof(times));
 51     for(int i = 1; i<=n; i++)
 52         dis[i] = LNF;
 53 
 54     queue<int>Q;
 55     Q.push(st);
 56     inq[st] = 1;
 57     dis[st] = 0;
 58     while(!Q.empty())
 59     {
 60         int u = Q.front();
 61         Q.pop(); inq[u] = 0;
 62         for(int i = head[u]; i!=-1; i = edge[i].next)
 63         {
 64             int v = edge[i].to;
 65             if(dis[v]>1LL*dis[u]+1LL*edge[i].w)
 66             {
 67                 dis[v] = 1LL*dis[u]+1LL*edge[i].w;
 68                 if(!inq[v])
 69                 {
 70                     Q.push(v);
 71                     inq[v] = 1;
 72                 }
 73             }
 74         }
 75     }
 76 }
 77 
 78 int main()
 79 {
 80     int T;
 81     scanf("%d",&T);
 82     while(T--)
 83     {
 84         init();
 85         scanf("%d%d", &n, &m);
 86         for(int i = 0; i<m; i++)
 87         {
 88             int u, v, w;
 89             scanf("%d%d%d", &u, &v, &w);
 90             add(u,v,w);
 91         }
 92 
 93         spfa(1, dis1);
 94         memset(head, -1, sizeof(head));
 95         for(int i = 0; i<m; i++)
 96             add(edge[i].to, edge[i].from, edge[i].w);
 97         spfa(1, dis2);
 98 
 99         LL ans = 0;
100         for(int i = 1; i<=n; i++)
101             ans += dis1[i]+dis2[i];
102 
103         printf("%lld\n", ans);
104     }
105 }
View Code

 

POJ1511 Invitation Cards —— 最短路spfa

标签:nes   ring   vol   img   clu   tor   tin   i++   class   

原文地址:http://www.cnblogs.com/DOLFAMINGO/p/7610536.html

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