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<cstdio>
#include<cstring>
#include<queue> using namespace std; typedef pair<int,int>p;
typedef long long ll;
struct Node
{
int y,next;
int val;
}node[][]; int cnt[],head[][]; void add(int x,int y,int val,int t)
{
node[t][++cnt[t]].y=y;
node[t][cnt[t]].val=val;
node[t][cnt[t]].next=head[t][x];
head[t][x]=cnt[t];
}
int t;
bool vis[];
ll dist[][];
priority_queue<p,vector<p>,greater<p> >que;
void dijstra(int t)
{
memset(vis,,sizeof(vis));
while(!que.empty())que.pop();
memset(dist[t],0x3f,sizeof(dist[t]));
que.push(p(,));
while(!que.empty())
{
p tmp = que.top();
que.pop();
int s=tmp.second;
int v=tmp.first;
if(vis[s])continue;
vis[s] = ;
dist[t][s]= v;
for(int i=head[t][s];i;i=node[t][i].next)
{
int to=node[t][i].y;
if(dist[t][to] > 1ll*(v+node[t][i].val))
{
que.push(p(v+node[t][i].val,to));
}
}
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
int p,q;
scanf("%d%d",&p,&q);
cnt[]=cnt[]=;
memset(head,,sizeof(head));
for(int i=;i<=q;i++)
{
int u,v,val;
scanf("%d%d%d",&u,&v,&val);
add(u,v,val,);
add(v,u,val,);
}
dijstra();
dijstra();
ll ans = ;
for(int i=;i<=p;i++)
{
ans += dist[][i] + dist[][i];
}
printf("%lld\n",ans);
}
}

Invitation Cards POJ - 1511 (双向单源最短路)的更多相关文章

  1. Invitation Cards POJ - 1511

    题目链接:https://vjudge.net/problem/POJ-1511 思路:题目意思就是,从1出发到所有城市,再从所有城市回到1的最短时间. 那么我们只要正跑一次图,然后反向存边,再跑一次 ...

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

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

  3. POJ 1511 Invitation Cards ( 双向单源最短路 || 最小来回花费 )

    题意 : 给出 P 个顶点以及 Q 条有向边,求第一个点到其他各点距离之和+其他各点到第一个点的距离之和的最小值 分析 : 不难看出 min( 第一个点到其他各点距离之和+其他各点到第一个点的距离之和 ...

  4. (最短路 SPFA)Invitation Cards -- poj -- 1511

    链接: http://poj.org/problem?id=1511 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82829#probl ...

  5. Invitation Cards POJ 1511 SPFA || dij + heap

    http://poj.org/problem?id=1511 求解从1去其他顶点的最短距离之和. 加上其他顶点到1的最短距离之和. 边是单向的. 第一种很容易,直接一个最短路, 然后第二个,需要把边反 ...

  6. POJ - 2253 Frogger 单源最短路

    题意:给定n个点的坐标,问从第一个点到第二个点的最小跳跃范围.d(i)表示从第一个点到达第i个点的最小跳跃范围. AC代码 #include <cstdio> #include <c ...

  7. [10.26_P2] 最短路 (单源最短路应用)

    单源最短路问题拓展 Description 给你一张图,图上有 n 个点,m 条边,要你找到两个点,使其最短路恰好包含给定的 k 个点.输出这条最短路的长度,输入保证有解. 输入格式 第一行两个数 n ...

  8. 牛客编程巅峰赛S1第6场 - 黄金&钻石&王者 C.星球游戏 (单源最短路,Dijkstra)

    题意:有\(n\)个点,\(m\)条双向边,两个方向的权值都是相等的,可以从\(A\)中的某个点出发走到\(B\)中的某个点,求所有路径中的最短距离,如果A和B中没有点联通,则输出\(-1\). 题解 ...

  9. 洛谷 P5837 [USACO19DEC]Milk Pumping G (单源最短路,dijkstra)

    题意:有一\(n\)个点,\(m\)条边的双向图,每条边都有花费和流量,求从\(1\)~\(n\)的路径中,求\(max\frac{min(f)}{\sum c}\). 题解:对于c,一定是单源最短路 ...

随机推荐

  1. 野路子码农系列(1) 创建Web API

    新工作正式开始了2天,由于客户暂时还没交接数据过来,暂时无事可做.恰逢政佬给某超市做的商品图像识别的项目客户催收了,老板要求赶紧搞个API,于是我就想我来试试吧. 说起API,我其实是一窍不通的,我对 ...

  2. DirectX11 With Windows SDK--26 计算着色器:入门

    前言 现在开始迎来所谓的高级篇了,目前计划是计算着色器部分的内容视项目情况,大概会分3-5章来讲述. DirectX11 With Windows SDK完整目录 Github项目源码 欢迎加入QQ群 ...

  3. 使用百度云 BOS 和 C# SDK 开发数据存储

    Ø  简介 本文主要介绍如何使用百度云的 C# SDK 操作 BOS(Baidu Object Storage/百度对象存储),以及常见问题和解决办法.本文将以以下几点展开学习: 1.   基本介绍 ...

  4. <发条游戏设计>粗翻——序言、

    序言——————————————————————— 优雅 就像在英语里的很多单词一样,“优雅”有着一些不同意义的解释.一些习惯性的用法常常带有“美”的意思:例如“她穿着一条优雅的裙子”,代表着一种完全 ...

  5. Java读取一个文本文件拼接成一个字符串(readFileToString)

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.I ...

  6. 论文翻译:Ternary Weight Networks

    目录 Abstract 1 Introduction 1.1 Binary weight networks and model compression 2 Ternary weight network ...

  7. 2018-2019-2 20165231 王杨鸿永 Exp6 信息搜集与漏洞扫描

    实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 2.实践内容 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具体服 ...

  8. nginx,作为前端的你会多少?

    --现在阅读的你,如果是个FE,相信你不是个纯切图仔.反之,如果是,该进阶了,老铁! 前端的我们,已经不仅仅是做页面,写样式了,我们还需要会做相关的服务器部署.废话不多说,下面就从前端的角度来讲以下n ...

  9. SpringBoot@GeneratedValue 四种参数

    按照大家学习SpringBoot的经验来看, SpringBoot的@GeneratedValue 是不需要加参数的,但是如果数据库控制主键自增(auto_increment), 不加参数就会报错.( ...

  10. ext window嵌jsp页面自适应

    //定义window调用方法传入jsp所需参数function getWindow(obj,obj1,obj2,obj3,obj4,obj5,obj6,obj7,obj8,obj9){ Ext.def ...