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. Spring → 03:核心机制

    一.控制反转 1.1.控制反转的概念 (1).Inverse of Controller被称为控制反转或反向控制,其实真正体现的是“控制转移”.(2).所谓的控制指的是负责对象关系的指定.对象创建.初 ...

  2. 使用Data Lake Analytics从OSS清洗数据到AnalyticDB

    前提 必须是同一阿里云region的Data Lake Analytics(DLA)到AnalyticDB的才能进行清洗操作: 开通并初始化了该region的DLA服务: 开通并购买了Analytic ...

  3. PLAY2.6-SCALA(十) 模板引擎Twirl

    一.语法 1.@ 它是一个特殊的字符,表示动态声明的开始.对于简单的动态声明结尾可以从代码块中自动推断结尾,对于复杂的表达式通常加上() Hello @(customer.firstName + cu ...

  4. Linux 下的mysql+centos7+主从复制

    mysql+centos7+主从复制   MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公 ...

  5. request header....

    root@xxx# curl -i --get --include 'http://ali-barcode.showapi.com/barcode?code=6938166920785' -H 'Au ...

  6. 2016年中国的SaaS服务商企业研究

    近年来,随着中国人口红利的消退及移动互联网红利的凸显,让中国的To C创业,尤其是O2O领域的创业经历了一波高潮.2015年末,一场"资本寒冬"让O2O创业趋于理性,但SaaS及T ...

  7. [考试维护]之IIS发布 标签: iis 2015-06-07 22:11 627人阅读 评论(18) 收藏

    考试维护也进行了一段时间了,总结一下这段时间学习到的东西,今天写一下在服务器上如何发布IIS,一开始,我们准备了两台服务器,一台Win Server2003的服务器(IIS版本6.0),另一台是Win ...

  8. Android系列之Android 命令行手动编译打包详解

    Android 命令行手动编译打包过程图 [详细步骤]: 1使用aapt生成R.java类文件:  例:  E:\androidDev\android-sdk-windows2.2\tools> ...

  9. TCP/IP 、HTTP和SOCKET

    TCP/IP协议概念 TCP/IP(Transmission Control Protocol/Internet Protocol)的简写,中文译名为传输控制协议/因特网互联协议,又叫网络通讯协议,这 ...

  10. input上传图片并预览

    首先说一下input 大家都知道上传文件,图片是通过input 的file进行上传的. 1. 首先是样式 大家都知道input在HTML的代码为 <input type="file&q ...