问从1号点到各个点的距离+各个点到1号点之间的距离和的最小值

详解键连接https://www.cnblogs.com/csx-zzh/p/13411588.html

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 <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
#define ll long long
#define inf 0x7fffffff
#define N 1212121
int n,m,t,tot;
struct node
{
int u,v,w,next;
}e[N];
int head[N];
int dis[N];
int vis[N];
void add(int u,int v,int w)
{
e[tot].u=u;
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot++;
}
ll spfa(int s)
{
for(int i=1;i<=n;i++)
{
vis[i]=0;
dis[i]=inf;
}
vis[s]=1;
dis[s]=0;
queue<int >q;
q.push(s);
while(!q.empty())
{
int now=q.front();
q.pop();
vis[now]=0;
for(int i=head[now];i!=-1;i=e[i].next)
{
if(dis[e[i].v]>dis[now]+e[i].w)
{
dis[e[i].v]=dis[now]+e[i].w;
if(!vis[e[i].v])
{
vis[e[i].v]=1;
q.push(e[i].v);
}
}
}
}
ll ans=0;
for(int i=1;i<=n;i++)
{
if(dis[i]!=inf)
ans+=dis[i];
}
return ans;
}
int main()
{
scanf("%d",&t);
while(t--)
{
int u,v,w;
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
tot=0;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
ll sum=spfa(1);
tot=0;
memset(head,-1,sizeof(head));
for(int i=0;i<m;i++)
{
u=e[i].u;
v=e[i].v;
w=e[i].w;
add(v,u,w);
}
sum+=spfa(1);
printf("%lld\n",sum);
}
}

E - E(最短路解决源点到多点,多点到源点的和(有向图))的更多相关文章

  1. D - D (最短路解决源点到多点,多点到源点的和(有向图))

    问从1号点到各个点的距离+各个点到1号点之间的距离和的最小值 In the age of television, not many people attend theater performances ...

  2. HDU 2680 最短路 迪杰斯特拉算法 添加超级源点

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. P1828 香甜的黄油 Sweet Butter 最短路 寻找一个点使得所有点到它的距离之和最小

    P1828 香甜的黄油 Sweet Butter 闲来无事 写了三种最短路(那个Floyed是不过的) 题目描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1 ...

  4. POJ 1511 最短路spfa

    题很简单 就是有向图中求给出的源点到其余所有点的最短路的和与其余所有点到源点的最短路之和 一开始以为dij对于正权图的单源最短路是最快的 写了一发邻接表的dij 结果超时 把所有的cin改成scanf ...

  5. Wormholes 最短路判断有无负权值

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  6. 详解zkw算法解决最小费用流问题

    网络流的一些基本概念 很多同学建立过网络流模型做题目, 也学过了各种算法, 但是对于基本的概念反而说不清楚. 虽然不同的模型在具体叫法上可能不相同, 但是不同叫法对应的思想是一致的. 下面的讨论力求规 ...

  7. Bellman-Ford 求含负权最短路

    该算法详解请看   https://www.cnblogs.com/tanky_woo/archive/2011/01/17/1937728.html 单源最短路   当图中存在负权边时 迪杰斯特拉就 ...

  8. 最短路之SPFA算法

    部分来自:http://blog.csdn.net/juststeps/article/details/8772755 求最短路径的算法有许多种,除了排序外,恐怕是OI界中解决同一类问题算法最多的了. ...

  9. 关于dijkstra的优化 及 多源最短路

    先来看这样一道题目 给你N个点,M条双向边,要求求出1号点到其他所有点的距离.其中 2 <= N <= 1e5,  1 <=M <= 1e6. 对于这样的一道题目 我们当然不可 ...

随机推荐

  1. WPF学习笔记01_XAML之简介

    简介 XAML (发音"zammel" 咋么儿),用于实例化.NET对象的标记语言,主要用于构造WPF的用户界面.类似html标记语言. 通过XAML,程序员可以用代码的方式对界面 ...

  2. Centos镜像国内最全下载地址

    CentOS 官方下载地址:https://www.centos.org/download/Centos国内下载源http://man.linuxde.net/download/CentOShttp: ...

  3. maven 无法导入ojdbc 的jar包 解决方法

    由于maven无法在线安装ojdbc包,所有先在我们需要手动导入. 准备环境: 1.系统需要配置好jdk以及maven环境. 2.ojdbc的jar包,记住jar的路径,我的路径是:E:\jdbc\o ...

  4. 你还不知道mysql中空值和null值的区别吗?

    前言 最近发现带的小伙伴写sql对于空值的判断方法不正确,导致程序里面的数据产生错误,在此进行一下整理,方便大家以后正确的判断空值.以下带来示例给大家进行讲解. 建表 create table tes ...

  5. Shiro的认证与授权

    shiro实战教程 一.权限管理 1.1什么是权限管理 基本上涉及到用户参与的系统都需要进行权限管理,权限管理属于系统安全的范畴,权限管理实现对用户访问系统的控制,按照安全规则或者安全策略控制用户可以 ...

  6. 【Java】标识符

    一.标识符 文章目录 一.标识符 1.标识符的命名规则 2.关键字.保留字.特殊值 3.code Java 对各种变量.方法和类等要素命名时使用的字符序列称为标识符.简单的说,凡是程序员自己命名的部分 ...

  7. Springboot之文件监控

    背景:在实际环境部署构成中,由于特殊网络环境因素,有很多服务器之间的网络都是单向的,不能互相访问的,只有通过特定技术手段做到文件的单项摆渡,这就需要在两台服务器上分别写序列化程序和反序列化程序,这里不 ...

  8. java锁的对象引用

    当访问共享的可变数据时,通常需要同步.一种避免使用同步的方式就是不共享数据. 如果数据仅在单线程内访问,就不需要同步,这种技术称为"线程封闭",它是实现线程安全性最简单方式之一. ...

  9. os.system('cmd')在linux和windows系统下返回值的差异

    今天,用os.system('cmd')分别在windows和linux平台上执行同一ping命令,命令执行失败时返回码不同,windows为1,而linux下返回为256,如下: linux下: & ...

  10. oracle新增ID主键列,如何补全旧数据的ID值

    1.创建SEQUENCE CREATE SEQUENCE MONKEY.TEST_ADD_IDCOL_ID CACHE 100; 2.新增表栏位 ALTER TABLE MONKEY.TEST_ADD ...