题目如下:

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 Output
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点到各个点的最短路之和

再加上

各个点到1点的最短路之和

所有我们可以 建反图 + 跑两边SPFA就可以解决了!

AC代码如下:(ps:关闭输入输出流也可能会T,建议直接C输入输出写法)

#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#define ll long long
#define buff                     \ios::sync_with_stdio(false); \cin.tie(0);                  \cout.tie(0)
#define endl "\n"
using namespace std;
const int N = 1000000 + 9;
int n, m;
int h[N], ne[N], e[N], idx;
int h2[N], ne2[N], e2[N], idx2;
ll w[N], w2[N];
int dist[N], dist2[N];
bool st[N];
void add(int a, int b, int c)
{e[idx] = b;w[idx] = c;ne[idx] = h[a];h[a] = idx++;
}
void add2(int a, int b, int c)
{e2[idx2] = b;w2[idx2] = c;ne2[idx2] = h2[a];h2[a] = idx2++;
}
void spfa()
{for (int i = 1; i <= n; i++){dist[i] = 0x3f3f3f3f;st[i] = 0;}queue<int> q;q.push(1);dist[1] = 0;st[1] = 1;while (!q.empty()){int t = q.front();q.pop();st[t] = 0;for (int i = h[t]; i != -1; i = ne[i]){int j = e[i];if (dist[j] > dist[t] + w[i]){dist[j] = dist[t] + w[i];if (!st[j]){st[j] = 1;q.push(j);}}}}
}
void spfa2()
{for (int i = 1; i <= n; i++){st[i] = 0;dist2[i] = 0x3f3f3f3f;}queue<int> q;q.push(1);dist2[1] = 0;st[1] = 1;while (!q.empty()){int t = q.front();q.pop();st[t] = 0;for (int i = h2[t]; i != -1; i = ne2[i]){int j = e2[i];if (dist2[j] > dist2[t] + w2[i]){dist2[j] = dist2[t] + w2[i];if (!st[j]){st[j] = 1;q.push(j);}}}}
}
void init()
{idx = 0;idx2 = 0;memset(h, -1, sizeof h);memset(h2, -1, sizeof h2);
}
void solve()
{cin >> n >> m;init();for (int i = 1; i <= m; i++){int a, b;ll c;//cin >> a >> b >> c;scanf("%d %d %lld",&a,&b,&c);add(a, b, c);add2(b, a, c);}spfa();spfa2();ll ans = 0;for (int i = 1; i <= n; i++){ans += dist[i] + dist2[i];}printf("%lld\n",ans);
}
signed main()
{//buff;int t;cin >> t;while (t--)solve();
}

Invitation Cards(建反图 + 跑两遍SPFA)相关推荐

  1. Magic Potion(最大流,跑两遍网络流或者加一个中转点)

    Magic Potion http://codeforces.com/gym/101981/attachments/download/7891/20182019-acmicpc-asia-nanjin ...

  2. Luogu P1073 最优贸易【最短路/建反图】 By cellur925

    题目传送门 这么经典的题目,还是看了lyd的题解....唉难过. 一句话题意:在一张点有全都的图上找一条从1到n的路径,存在两个点p,q(p<q),使val[q]-val[p]最大. 给出的图是 ...

  3. Codeforces Round #588 (Div. 2) F. Konrad and Company Evaluation 图论 + 建反图 好题

    传送门 文章目录 题意: 思路: 题意: 给你一张nnn个点mmm条边的图,其中每个点iii初始编号为iii,边是有向的,方向为从编号大的指向编号小的.定义一个贡献为存在某三个点a,b,ca,b,ca ...

  4. 洛谷:P3243 [HNOI2015]菜肴制作(拓扑序列、建反图、贪心)

    美食家老嗨 题意: 很容易读偏,以为是求满足限制下的最小字典序. 要求是在满足所有限制后,优先做 1,此外优先做 2 - -- 摘自xyz32768 C o d e : Code: Code: #in ...

  5. POJ 1511 Invitation Cards——Dijkstra优先队列优化+反向建图

    [题目描述] In the age of television, not many people attend theater performances. Antique Comedians of M ...

  6. Invitation Cards POJ - 1511 SPFA(dijkstra+反向建图+邻接表(下标过大)+输入输出用stdio(iostream超时))

    题目大意: 有编号1-P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接 到达终点站,是单向的,每条路线有它自己的车费.有P个人早上从1出发 ,他们要到达每一个公交站点, 然后到了晚上再返回点 ...

  7. 【POJ - 1511】 Invitation Cards(Dijkstra + 反向建图 多源到单源最短路的处理)

    题干: In the age of television, not many people attend theater performances. Antique Comedians of Mali ...

  8. 2152 Balloons(两遍bfs求图的连通块)

    2152 Balloons(两遍bfs求图的连通块) Problem Description Both Saya and Kudo like balloons. One day, they heard ...

  9. lisp遍历表中所有顶点_三十张图片让你彻底弄明白图的两种遍历方式:DFS和BFS...

    1 引言   遍历是指从某个节点出发,按照一定的的搜索路线,依次访问对数据结构中的全部节点,且每个节点仅访问一次.   在二叉树基础中,介绍了对于树的遍历.树的遍历是指从根节点出发,按照一定的访问规则 ...

最新文章

  1. LeetCode中等题之重排数字的最小值
  2. 个人计算机用户隐私保护全接触(2)
  3. 又遇到问题:wrong ELF class: ELFCLASS32 in Unknown on line
  4. mysql的覆盖索引原理_「Mysql索引原理(七)」覆盖索引
  5. 【转】C++11多线程的基本使用
  6. 设置图的位置_消防泵房内设备、管网、阀门的设置及系统图
  7. csv文件 内容转义_CSV文件如何同时转义逗号和双引号?
  8. Colder框架硬核更新(Sharding+IOC)
  9. java文件读写操作类
  10. html怎么在字体中加波浪线,CSS3实现文字波浪线效果
  11. MCI:移动持续集成在大众点评的实践
  12. Linux开发cocos2dx程序环境搭建
  13. 导出一条数据_来自小师弟的灵魂拷问之数据泵导出丢失的那些数据量去哪了?...
  14. 63.magento 后台重置密码
  15. 计算机用的代码怎么写,什么是代码,代码怎么写,怎么样写入电脑
  16. php怎么seo,怎样学习seo
  17. centos7下安装airflow
  18. 风险评估(Risk Assessment)
  19. insert的语句的三种方式
  20. 如何把excel表格的数据导入到MATLAB中去

热门文章

  1. java 从键盘中读取字符流 自定义异常
  2. ACM比赛技巧之文件数据输入与输出
  3. 【MATLAB】主要功能
  4. Take C# 8.0 for a spin
  5. array.prototype.map()如何工作
  6. gitlab定期备份_如何在一分钟内让GitLab为您做定期工作
  7. fcn从头开始_从头开始有营销问题
  8. flutter 生成文档_flutter 如何实现文件读写(使用篇)
  9. mac安装openjdk
  10. 你想知道的Spring框架配置使用流程和依赖注入!