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. PHP生成唯一的促销/优惠/折扣码,由字母和数字组成。

    首先我们先搞清楚什么是促销/优惠/折扣码?它有什么用作: 每一个电子商务网站,现在有一种或多种类型的优惠/折扣/优惠券系统,给大家分享一下如何在PHP生成唯一的促销/折扣码.主要是实现一个优惠码系统, ...

  2. 【插拔式】分页+bootstrap4(开源)

    1:分页源码 class Pagination(object): def __init__(self, PagerCount,Pages, perPageItemNum, maxPageNum): # ...

  3. hdu4267 线段树

    开始敲了一发线段树,觉得可以暴力一点的过,tle了.后来进行修改,发现了问题. 后来一看大神的做法,由于1<=k<=10,所以对于不同的k,有55个余,找答案的时候只要找不同的k值满足条件 ...

  4. 如何用好消息推送(push)做APP运营

    作为移动端APP产品运营最重要的运营手段,消息推送(push)被越来越多的APP厂商所重视,在信息泛滥的移动互联网时代,手机APP应用安装得越来越多,小小的手机屏幕每天收到的消息推送也越来越多,站在用 ...

  5. SDUT-2144_最小生成树

    数据结构实验之图论九:最小生成树 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 有n个城市,其中有些城市之间可以修建公 ...

  6. Extended Traffic

    题目链接 题意:有n个路口,m条通路,如果经过一条路则会得到(终点 - 起点)^3的权值,求从1点到其他点的最小权值,如果权值小于3或无法到达输出'?'. 题解:因为权值可能为负,所以用SPFA来解题 ...

  7. 【NS2】NS2中802.11代码深入理解—packet传输的流程(转载)

    如何传送一个封包(How to transmit a packet?)首先,我们要看的第一个function是在mac-802_11.cc内的recv( ),程式会先判断目前呼叫recv( )这个pa ...

  8. 关于使用JavaMail发送邮件抛出java.lang.NoSuchMethodError: com.sun.mail.util.TraceInputStream.<init>(Ljava异常的解决方法

    我们在使用JavaMail时有可能会如下异常: Exception in thread "main" java.lang.NoSuchMethodError: com.sun.ma ...

  9. Python类型模块:types

    types模块中定义了Python中所有的类型,包括NoneType,  TypeType,  IntType,  FloatType,  BooleanType,  BufferType,  Bui ...

  10. python 清空文件夹

    #!/usr/bin/env python# -*- coding:utf-8 -*-import os def del_file(path): for i in os.listdir(path): ...