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 数据量太大必须用堆优化。。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<map>
#include<queue>
using namespace std;
#define MAXN 1000010
#define INF 0x3f3f3f3f
typedef long long LL;
/*
两次 Dijkstra
结点数目太多 必须优化
*/
struct qnode
{
LL v,c;
qnode(LL _v=,LL _c=):v(_v),c(_c){}
bool operator <(const qnode &r)const
{
return c>r.c;
}
};
struct Edge
{
LL v,cost;
Edge(LL _v=,LL _cost=):v(_v),cost(_cost){}
};
struct node
{
LL f,t,d;
};
vector<Edge> E[MAXN];
node tmp[MAXN];
bool vis[MAXN];
LL dist[MAXN],n,m;
void Dijkstra(LL n,LL start)
{
memset(vis,false,sizeof(vis));
for(LL i=;i<=n;i++)
dist[i] = INF;
dist[start] = ;
priority_queue<qnode> q;
while(!q.empty()) q.pop();
q.push(qnode(start,));
qnode tmp;
while(!q.empty())
{
tmp = q.top();
q.pop();
LL u = tmp.v;
if(vis[u]) continue;
vis[u] = true;
for(LL i=;i<E[u].size();i++)
{
LL v = E[u][i].v;
LL cost = E[u][i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost)
{
dist[v] = dist[u]+cost;
q.push(qnode(v,dist[v]));
}
}
}
}
void Add(LL u,LL v,LL cost)
{
E[u].push_back(Edge(v,cost));
} int main()
{
LL t;
cin>>t;
while(t--)
{
scanf("%lld%lld",&n,&m);
LL ans=;
for(LL i=;i<=n;i++) E[i].clear();
for(LL i=;i<m;i++)
{
scanf("%lld%lld%lld",&tmp[i].f,&tmp[i].t,&tmp[i].d);
Add(tmp[i].f,tmp[i].t,tmp[i].d);
}
Dijkstra(n,);
for(LL i=;i<=n;i++)
{
ans+=dist[i];
E[i].clear();
}
for(LL i=;i<m;i++)
{
Add(tmp[i].t,tmp[i].f,tmp[i].d);
}
Dijkstra(n,);
for(LL i=;i<=n;i++)
{
ans+=dist[i];
}
printf("%lld\n",ans);
}
return ;
}

J - Invitation Cards 最短路的更多相关文章

  1. D - Silver Cow Party J - Invitation Cards 最短路

    http://poj.org/problem?id=3268 题目思路: 直接进行暴力,就是先求出举行party的地方到每一个地方的最短路,然后再求以每一个点为源点跑的最短路. 还有一种方法会快很多, ...

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

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

  3. HDU 1535 Invitation Cards (最短路)

    题目链接 Problem Description In the age of television, not many people attend theater performances. Anti ...

  4. POJ1511 Invitation Cards —— 最短路spfa

    题目链接:http://poj.org/problem?id=1511 Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Tota ...

  5. POJ-1511 Invitation Cards( 最短路,spfa )

    题目链接:http://poj.org/problem?id=1511 Description In the age of television, not many people attend the ...

  6. hdu1535 Invitation Cards 最短路

    有一张图,若干人要从不同的点到同一个中间点,再返回,求总费用最小 中间点到各个点最小费用是普通的最短路 各个点到中间点最小费用其实就是将所有路径反向建边之后中间点到各个点的最小费用,同样用最短路就可以 ...

  7. J - Invitation Cards

    题目大意:邀请卡 在电视的时代,没有多少人会去剧院观看演出.古老的喜剧演员 Malidinesia知道这个事实.他们想传播戏剧尤其是古老的戏剧,他们在邀请卡上打印必要的信息和一些节目,一些学生被雇佣过 ...

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

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

  9. Invitation Cards POJ - 1511 (双向单源最短路)

    In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...

随机推荐

  1. fprintf写入字符串入文件/fread读取文件内的字符串

    #include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { FILE * ...

  2. C#命名空间 using的用法

    using的用法: 1. using指令:引入命名空间 这是最常见的用法,例如: using System; using Namespace1.SubNameSpace; 2. using stati ...

  3. idea之映射servlet问题

    方式一: 配置web.xml如下节点,事例如下: servlet>       <servlet-name>HelloWorld</servlet-name>       ...

  4. IDE 快捷键汇总

    一.webstorm div.head + tab-------------------- div#btn + tab---------------------------- div.head#btn ...

  5. MyElipse如何添加Emmet插件

    把这个jar文件放到myeclipse2014安装目录下dropins文件夹中,然后重启myeclipse即可. 可到window-->perferences里查看,如果成功则会看到emmet选 ...

  6. iOS规范化时间格式,object-C计算指定时间与当前的时间差

    object-c计算指定时间与当前的时间差 头文件(.h): #import <Foundation/Foundation.h> @interface LuDate : NSDate +( ...

  7. 1807. [NOIP2014]寻找道路P2296 寻找道路

    题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条 ...

  8. 使用libpqxx访问PostgreSQL数据库(mingw编译libpqxx)

    编译前准备    1. 安装mingw 安装mingw(不管是直接安装mingw还是其他如code::blocks附带安装的mingw),输入:gcc -v可显示如下图的版本信息,我的版本是mingw ...

  9. Farseer.net轻量级开源框架 入门篇:逻辑层的选择

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 入门篇:增.删.改.查操作演示 下一篇:Farseer.net轻量级开源框架 入门 ...

  10. Java 基础入门随笔(6) JavaSE版——数组操作

    1.数组 概念:同一种类型数据的集合.其实就是数组就是一个容器. 好处:可以自动给数组中的元素从0开始编号,方便操作这些元素. 格式: ①. 元素类型[] 数组名 = new 元素类型[元素个数或数组 ...