D - Invitation Cards

 D - Invitation Cards

Time Limit:8000MS     Memory Limit:262144KB     64bit IO Format:%lld & %llu

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

 

//题目的意思是:第一行、一个整数N ,代表 N 个测试案例,第二行 ,两个整数 1<=P,Q<=一百万。意思是P个点,Q条 "有向边" 。然后就是Q行有向边和权值,求从点 1 到所有点的最小路径和,再加上从所有点到点 1 的最小路径和,数据保证所有点都能连通。

 

 

//牛逼的 spfa 算法,今天学习了spfa算法,很强大,bian数组是存放有向边并且成为链表的节点的,headlist的作用是建立链表,存放头节点的。d数组是存放到任一点最短路径长度的,vis不知道干嘛的,去掉也对,而且更快。。。我看到想了很久。。。作用是啥?  数据很大,0x  f 用7个是错的,8个才对。然后逆序的就是将有向边反向,从 点1 走到所有点最小路径之和,好了,具体看代码吧。

 

71676kb 1907ms(去掉vis数组1797ms...)

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <queue>
 4 using namespace std;
 5 
 6 #define inf 0xffffffff
 7 #define MAX 1000010
 8 
 9 struct Bian
10 {
11     int e;
12     __int64 w;
13     int next;
14 }bian[2][MAX];
15 __int64 d[MAX];
16 bool vis[MAX];
17 __int64 headlist[2][MAX];
18 int n,m;
19 
20 void spfa(bool cap)
21 {
22     int i,x,y;
23     queue<int> Q;
24     for (i=1;i<=n;i++)
25     {
26         d[i]=inf;
27         vis[i]=0;
28     }
29     d[1]=0;
30     vis[1]=1;
31     Q.push(1);
32     while (!Q.empty())
33     {
34         x=Q.front();
35         Q.pop();
36         vis[x]=0;
37         for (i=headlist[cap][x];i!=-1;i=bian[cap][i].next)
38         {
39             y=bian[cap][i].e;
40             if (d[y]>d[x]+bian[cap][i].w)
41             {
42                 d[y]=d[x]+bian[cap][i].w;
43                 if (!vis[y])
44                 {
45                     vis[y]=1;
46                     Q.push(y);
47                 }
48             }
49         }
50     }
51 }
52 
53 int main()
54 {
55     int N;
56     int a,b,i;
57     __int64 c;
58     __int64 ans;
59     scanf("%d",&N);
60     while (N--)
61     {
62         scanf("%d%d",&n,&m);
63 
64         for (i=1;i<=n;i++)    //初始化链表
65         {
66             headlist[0][i]=-1;
67             headlist[1][i]=-1;
68         }
69 
70         for (i=1;i<=m;i++)
71         {
72             scanf("%d%d%I64d",&a,&b,&c);
73             bian[0][i].e=b;                 //正序
74             bian[0][i].w=c;
75             bian[0][i].next=headlist[0][a];
76             headlist[0][a]=i;
77 
78             bian[1][i].e=a;                 //逆序
79             bian[1][i].w=c;
80             bian[1][i].next=headlist[1][b];
81             headlist[1][b]=i;
82         }
83         ans=0;
84         spfa(0);
85         for (i=1;i<=n;i++)
86             ans+=d[i];
87         spfa(1);
88         for (i=1;i<=n;i++)
89             ans+=d[i];
90         printf("%I64d\n",ans);
91     }
92     return 0;
93 }
View Code

 

posted @ 2016-07-28 11:00  happy_codes  阅读(169)  评论(0编辑  收藏  举报