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. 410 Split Array Largest Sum 分割数组的最大值

    给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小.注意:数组长度 n 满足以下条件:    1 ≤ n ≤ 1000 ...

  2. 实用and常用shell命令汇编

    很久没写blog了,基本都在用 github和笔记.现在将一些常用的shell并且很使用的shell用法分享一下: 分行读取,切割,计数: cat product.txt | while read l ...

  3. Linux 配置 nginx + php

    为什么!!!我配过的服务器已经有5.6个了吧,为什么每一次配置都能要了我的老命??这次写清楚过程,以后再要被配服务器坑,我特么要砍人了. 提示:测试网站能否访问的时候,最好关掉浏览器的缓存功能或者勤清 ...

  4. PHP fSQL Tutorial

    PHP fSQL Tutorial This tutorial is designed to give a brief overview of the PHP fSQL API since versi ...

  5. jsp学习笔记 - 内置对象 config

    1.将页面保存在 WEB-INF文件夹下是最安全的,一般不可见 可以通过映射路径来进行访问 2.通过config对象可以取得初始化的配置参数 String dbDriver = config.getI ...

  6. 语音跟踪:信号分解、锁相、鸡尾酒会效应、基于PR的信号分离

    NLP中关于语音的部分,其中重要的一点是语音信号从背景噪音中分离.比如在一个办公室场景中,有白天的底噪-类似于白噪音的噪音.空调的声音.键盘的啪啪声.左手边45度7米元的地方同事讨论的声音.右手边1. ...

  7. Python之字符串的特性及常用方法

    字符串的特性 索引: (索引是从0开始) s='hello'print(s[0])print(s[4])print(s[-1]) #拿出最后一个字符 hoo12345678截取s[start:stop ...

  8. CAD实现文档坐标到视区坐标的转换(com接口Delphi语言)

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  9. CAD绘制自定义实体(com接口)

    在cad使用过程中,用户可以绘制自定义实体.点击此处下载演示实例. 调用DrawCustomEntity函数,绘制一个自定义实体对象. 下面代码绘制一个自定义实体,C#代码实现如下: private ...

  10. 关于MD5解密网站。www.cmd5.com

    第一次听说这个网站,本人的名字居然也能够被解密,而且还是需要付费取得明文! 大家知道,md5加密是我们常用的加密方式,这个加密方式的好处在于不可逆.而且任何环境下算出的密文应该都是相同的,所以在大家登 ...