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. “System.Reflection.AmbiguousMatchException”类型的异常在 mscorlib.dll 中发生

    错误提示: “System.Reflection.AmbiguousMatchException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理. 发现不明确的匹配. 问题原 ...

  2. dirname(__FILE__) === __DIR__

    dirname(__FILE__) === __DIR__get_class($this) == __CLASS__

  3. 第13章 GPIO—位带操作

    第13章     GPIO—位带操作 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fire ...

  4. activiti发布APP时报错:关联的流程无效

    解决办法: 1.检查流程命名和任务命名以及其他命名中是否有特殊字符,有一些字符是不支持的.(中文是可以的,中文标点符号可能不行,我的经验是顿号会报错) 2.检查流程图,把鼠标放到每一根连接线上,观察它 ...

  5. Exp6

    实验内容 一.公开渠道信息搜集 本次信息搜集在metasploit平台上实现(使用msfconsole命令进入) 1.通过DNS和IP挖掘信息 (1)使用whois进行域名信息查询 使用原因:目前互联 ...

  6. 【转】基于Ubuntu Server16.04 安装Odoo11

    使用 非 root 用户 进行下面的测试: 本文使用 有sudo 权限的 odoo 用户进行测试()如果是 阿里云,可以先创建 odoo 用户 sudo adduser odoo 2:给root 权限 ...

  7. Linux 设置core dump

    Linux 设置core dump

  8. C语言与数据库操作入门

    https://blog.csdn.net/flyingqd/article/details/78763652 C语言与数据库操作入门(Win版) 2017年12月10日 17:30:17 阅读数:1 ...

  9. stl源码剖析 详细学习笔记stack queue

    // //  stack.cpp //  笔记 // //  Created by fam on 15/3/15. // // //---------------------------15/03/1 ...

  10. 利用Kinect实现用指尖隔空控制鼠标(源码放出)

    简介 此程序为利用Kinect实现用手指隔空控制鼠标,是我另一个项目的一部分,因为在另外那个项目中鼠标的click是通过一种特殊的方式实现的,因此这个程序只实现了用手控制鼠标的移动,并没有点击的功能. ...