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 思路:思路不难只要多建一个逆序图,求出他的最短路加上之前正序图的最短路,就是答案,难点只要是数据太大容易爆容量和超时。所以选择链式前向星+spfa来实现。
实现代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define ll long long
#define M 1000100
#define INF 0x3f3f3f3f
int dist[M],vis[M],head[M],x[M],y[M],z[M],cnt,n;
struct edge{
int to,w,next;
}edge[M];
void add(int u,int v,int w){
edge[cnt].next = head[u];
edge[cnt].to = v;
edge[cnt].w = w;
head[u] = cnt++;
}
void spfa(int s){
memset(vis,,sizeof(vis));
for(int i = ;i <= n; i++) dist[i] = INF;
dist[s] = ;
queue<int>q;
q.push(s);
while(!q.empty()){
int u = q.front(); q.pop();
vis[u] = ;
for(int k = head[u]; k ;k = edge[k].next){
int v = edge[k].to;
if(dist[v] > dist[u] + edge[k].w){
dist[v] = dist[u] + edge[k].w;
if(!vis[v]){
vis[v] = ;
q.push(v);
}
}
}
}
} int main(){
int t,m;
while(scanf("%d",&t)!=EOF){
while(t--){
memset(head,,sizeof(head));
memset(edge,,sizeof(edge));
scanf("%d%d",&n,&m);
cnt = ;
for(int i = ;i <= m; i++){
scanf("%d%d%d",&x[i],&y[i],&z[i]);
add(x[i],y[i],z[i]);
}
spfa();
ll ans = ;
for(int i = ;i <= n; i++){
ans += dist[i];
//cout<<dist[i]<<endl;
}
memset(head,,sizeof(head));
memset(edge,,sizeof(edge));
cnt = ;
for(int i = ;i <= m; i++){
add(y[i],x[i],z[i]);
}
spfa();
for(int i = ;i <= n; i++){
ans += dist[i];
//cout<<dist[i]<<endl;
}
cout<<ans<<endl;
}
}
}

poj 1511 Invitation Cards(最短路中等题)的更多相关文章

  1. POJ - 1511 Invitation Cards(Dijkstra变形题)

    题意: 给定一个有向图,求从源点到其他各点的往返最短路径和.且这个图有一个性质:任何一个环都会经过源点. 图中的节点个数范围:0-100w; 分析: 我们先可以利用Dijkstra算法求解从源点到其余 ...

  2. 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 / ...

  3. POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 16178   Accepted: 526 ...

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

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

  5. poj 1511 Invitation Cards (最短路)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 33435   Accepted: 111 ...

  6. [POJ] 1511 Invitation Cards

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

  7. poj1511/zoj2008 Invitation Cards(最短路模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds    ...

  8. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

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

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

随机推荐

  1. USB主机控制器ECHI

    USB主机控制器ECHI 2017年10月24日 15:44:11 阅读数:239 1. 主机控制器(Host Controller) • UHCI: Universal Host Controlle ...

  2. mssql2012的分页查询

    sql2102支持的分页查询 注意:以下都是先执行排序,再取行数据 select* from t_workers order by worker_id desc offset 3 rows   --先 ...

  3. linux下ipython无法保存历史记录

    在Centos7下使用ipython时,发现有个warning,提示无法保存历史记录 [root@localhost pip-]# ipython /usr/local/lib/python3./si ...

  4. [BZOJ4082][Wf2014]Surveillance[倍增]

    题意 给你一个长度为 \(len\) 的环,以及 \(n\) 个区间,要你选择尽量少的区间,使得它们完全覆盖整个环.问最少要多少个区间. \(len,n\leq 10^6\) . 分析 考虑普通的区间 ...

  5. java拦截器(Interceptor)学习笔记

    1,拦截器的概念    java里的拦截器是动态拦截Action调用的对象,它提供了一种机制可以使开发者在一个Action执行的前后执行一段代码,也可以在一个Action执行前阻止其执行,同时也提供了 ...

  6. BaseProxy:异步http/https中间人

    BaseProxy 异步http/https代理,可拦截并修改报文,可以作为中间人工具.仅支持py3.5+.项目地址:BaseProxy. 意义 BaseProxy项目的本意是为了使HTTP/HTTP ...

  7. 初级字典树查找在 Emoji、关键字检索上的运用 Part-2

    系列索引 Unicode 与 Emoji 字典树 TrieTree 与性能测试 生产实践 在有了 Unicode 和 Emoji 的知识准备后,本文进入编码环节. 我们知道 Emoji 是 Unico ...

  8. JNI探秘-----你不知道的FileInputStream的秘密

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 设计模式系列结束,迎来了LZ ...

  9. zabbix3.4 实现sendEmail邮件报警

    zabbix3.4实现sendEmail邮件报警 转发:https://www.cnblogs.com/pythonal/p/7813948.html sendEmail是一个轻量级,命令行的SMTP ...

  10. Keyshot+AD渲染PCB效果图

    Keyshot+AD渲染PCB效果图 1.前言 前些天,公司同事找到我说,公司的展会宣传册要更新的了,有几款新的产品需要更新添加上去,大部分的新产品都有实物demo,可以拍照修一下图弄上去.但不巧,其 ...