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 题目大意:有p个城市,q条单向边,现在求点1到其他所有点在返回到1的距离之和的最小值。
思路:第一遍正向SPFA dis数组代表点1到其他所有点的最短距离,之后将每一条边反转,再求一遍SPFA,则dis数组代表除点1之外所有点返回到点1的最短距离
注意:由于输入数据量比较大用cin,cout妥妥的TLE,(scanf printf 也能过)。我这里就顺手打了个简单的读入
 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<queue> using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6 + ;
struct node {
LL to, cost;
node() {}
node(LL a, LL b) :to(a), cost(b) {}
};
struct Task {
LL from, to, cost;
}rs[maxn];
vector<node>e[maxn];
LL n, m, T, dis[maxn], vis[maxn];
LL read() {
LL x = ;
char c = getchar();
while (c < '' || c > '')c = getchar();
while (c >= '' && c <= '') {
x = x * + c - '';
c = getchar();
}
return x;
}
void SPFA(int s)
{
for (int i = ; i <= n; i++) {
dis[i] = INF; vis[i] = ;
}
queue<int>Q;
vis[s] = ; dis[s] = ;
Q.push(s);
while (!Q.empty()) {
int t = Q.front(); Q.pop();
vis[t] = ;
for (int i = ; i < e[t].size(); i++) {
int tmp = e[t][i].to;
if (dis[tmp] > dis[t] + e[t][i].cost) {
dis[tmp] = dis[t] + e[t][i].cost;
if (!vis[tmp]) {
vis[tmp] = ;
Q.push(tmp);
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
T = read();
while (T--) {
n = read(), m = read();
for (int i = ; i <= n; i++)e[i].clear();//一定不能忘的初始化!
for (int i = ; i <= m; i++) {
rs[i].from = read(); rs[i].to = read(); rs[i].cost = read();
e[rs[i].from].push_back(node(rs[i].to, rs[i].cost));//正向加边
}
LL ans = ;
SPFA();
e[].clear();
for (int i = ; i <= n; i++) {
ans += dis[i]; e[i].clear();//再次初始化
}
for (int i = ; i <= m; i++)//逆向加边
e[rs[i].to].push_back(node(rs[i].from, rs[i].cost));
SPFA();
for (int i = ; i <= n; i++)ans += dis[i]; cout << ans << endl;
}
return ;
}

POJ 1511 Invitation Cards(逆向思维 SPFA)的更多相关文章

  1. (简单) POJ 1511 Invitation Cards,SPFA。

    Description In the age of television, not many people attend theater performances. Antique Comedians ...

  2. POJ 1511 Invitation Cards 正反SPFA

    题意:学生从A站到B站花费C元,将学生每天从‘1’号站送往其它所有站,再从所有站接回到‘1’号站,问着个过程最小花费是多少. 思路:因为数据很大所以要用SPFA,因为不仅要从1点连接各个点还要从各个点 ...

  3. POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)

    POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...

  4. POJ 1511 Invitation Cards (spfa的邻接表)

    Invitation Cards Time Limit : 16000/8000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) ...

  5. POJ 1511 Invitation Cards (最短路spfa)

    Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...

  6. Poj 1511 Invitation Cards(spfa)

    Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...

  7. POJ 1511 Invitation Cards 链式前向星+spfa+反向建边

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 27200   Accepted: 902 ...

  8. SPFA算法(2) POJ 1511 Invitation Cards

    原题: Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 31230   Accepted: ...

  9. [POJ] 1511 Invitation Cards

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 18198   Accepted: 596 ...

随机推荐

  1. PyCharm2019 永久激活

    <!-- 2019激活码 2019-06-21新更新 --> D00F1BDTGF-eyJsaWNlbnNlSWQiOiJEMDBGMUJEVEdGIiwibGljZW5zZWVOYW1l ...

  2. MSSQL → 01:SQLServer 2008概述及安装

    据库的发展史 在人类诞生以来,就有记录数据的需求,在远古时代就有了结绳记事的故事,而随着科技的进步,我们记录数据的方式也发生了天翻地覆的变化,从效率低.规模小.不能适应信息高速发展的需要的手工或者简单 ...

  3. NodeJS基础之Express路由和中间件

    路由 路由是指如何定义应用的端点(URIs)以及如何响应客户端的请求. 路由是由一个 URI.HTTP 请求(GET.POST等)和若干个句柄组成,它的结构如下: app.method(path, [ ...

  4. linux的简单操作

    查看当前用户who am i 创建用户:sudo adduser lilei然后输入密码 查看用户:ls /home 用新用户登陆:su -l lilei 查看所属用户组:groups 用户名 新建文 ...

  5. SQL Server 记录(更新中...)

    sys.databases 显示所有数据库信息 sys.tables 显示当前数据库所有的表的信息 Go 向 SQL Server 实用工具发出一批 Transact-SQL 语句已结束的信号,Go本 ...

  6. beanstalkd 启动跟停止

    启动命令: nohup /usr/bin/beanstalkd -l xxx.xxx.xxx.xxx -p 11300 & >> /dev/null 2>&1 正常启 ...

  7. 2018-12-24-win10-uwp-求两个矩形相连的几何

    title author date CreateTime categories win10 uwp 求两个矩形相连的几何 lindexi 2018-12-24 20:51:49 +0800 2018- ...

  8. shell学习(19)- find查找命令

    Linux find命令用来在指定目录下查找文件.任何位于参数之前的字符串都将被视为欲查找的目录名.如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件.并且将查找到的子目录 ...

  9. 用div漂浮快实现与表单无关的多文件上传功能。

    我项目有这个需求,多文件上传,而且要及时显示到表单上,这样的话就不能与表单相关. 由于我对前端不熟,我就实现了这么一个功能,通过button触发一个div漂浮块,然后多文件上传,之后通过js把文件名显 ...

  10. 基本的Sql编写注意事项

    基本的Sql编写注意事项 尽量少用IN操作符,基本上所有的IN操作符都可以用EXISTS代替. 不用NOT IN操作符,可以用NOT EXISTS或者外连接+替代. Oracle在执行IN子查询时,首 ...