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
Inputcopy | Outputcopy |
---|---|
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 | 46 210 |
题目大意:从1号点派出n-1个人去2,3...n个点,给出路径权重,问去和回的最小成本
思路:刚开始是想着能不能对所有的点都进行dijkstra,但稍加计算就可得知这种做法肯定会超时,另外Floyd也为O(n^3)也跑不出来,最后还是看了博客大佬的思路才猛然醒悟:对于从1出发去其他点跑一次dijkstra就能得到答案,而对于其他点来1这个点,我们只需要将路线反向输入再跑一次dijkstra就可以得到最终答案
注意事项:内存方面,采用了链式前向星来优化避免所需内存过大,时间方面使用了优先队列的dijkstra进行优化,输入方面cin,cout我都试了直接T掉,最后改scanf,printf就在1s多AC了,最后,对于加结果的变量ans,记得开long long就好了
AC代码如下
#include <iostream>
#include <queue>
using namespace std;
const int inf = 0x3f3f3f3f;
long long p, q;
int first[2000005];
int u[2000005];
int v[2000005];
int w[2000005];
int vis[2000005];
int d[2000005];
int cnt = 0;
struct edges//用来构造链式前向星的结构体
{
int to, next, cost;
} arr[2000005];
void add(int u, int v, int w)//链式前向星的添加边函数
{
arr[++cnt].to = v;
arr[cnt].next = first[u];
arr[cnt].cost = w;
first[u] = cnt;
}
struct node//用来构造优先队列的结构体
{
int d, now;//d代表此点目前权重值,now代表现在点的坐标
bool friend operator<(node a, node b)//小的先出列
{
return a.d > b.d;
}
node(int d, int now) : d(d), now(now){};
};
void dijkstra()
{
priority_queue<node> mhy;
mhy.push(node(0, 1));
while (!mhy.empty())
{
int now = mhy.top().now;
mhy.pop();
if (vis[now])
continue;
vis[now] = 1;
//当此点链接的最后一条边用完i会指向第0条边从而退出
for (int i = first[now]; i; i = arr[i].next) //此时i为边的编号
{
if (d[arr[i].to] > d[now] + arr[i].cost)
{
d[arr[i].to] = d[now] + arr[i].cost;
mhy.push(node(d[arr[i].to], arr[i].to));
}
}
}
}
void init()//初始化函数
{
for (int i = 1; i <= p; i++)
{
d[i] = inf;
vis[i] = first[i] = 0;
}
d[1] = 0;
cnt = 0;
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
long long ans = 0;
scanf("%d%d",&p,&q);
init();
for (int i = 0; i < q; i++)
{
scanf("%d%d%d", &u[i], &v[i], &w[i]);
add(u[i], v[i], w[i]);
}
dijkstra();
for (int i = 1; i <= p; i++)
{
ans += d[i];
}
init();//初始化后在跑一遍dijkstra
for (int i = 0; i < q; i++)
{
add(v[i], u[i], w[i]);//反向建边
}
dijkstra();
for (int i = 1; i <= p; i++)
{
ans += d[i];
}
cout<<ans<<endl;
}
return 0;
}