D - D (最短路解决源点到多点,多点到源点的和(有向图))
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
Output
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
struct node
{
int u,v,w,next;
}e[N];
int dis[N];
int vis[N];
int head[N];
int n,m,t,tot;
/*建立邻接表*/
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++;
}
/*spfa算法*/
ll spfa(int s)
{
for(int i=1;i<=n;++i)
{
dis[i]=inf;
vis[i]=0;
}
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 a,b,c;
tot=0;
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
ll sum=spfa(1);
memset(head,-1,sizeof(head));
tot=0;
for(int i=0;i<m;i++)
{
a=e[i].u;
b=e[i].v;
c=e[i].w;
add(b,a,c);
}
sum+=spfa(1);
printf("%lld\n",sum);
}
}
D - D (最短路解决源点到多点,多点到源点的和(有向图))的更多相关文章
- E - E(最短路解决源点到多点,多点到源点的和(有向图))
问从1号点到各个点的距离+各个点到1号点之间的距离和的最小值 详解键连接https://www.cnblogs.com/csx-zzh/p/13411588.html In the age of te ...
- HDU 2680 最短路 迪杰斯特拉算法 添加超级源点
Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- P1828 香甜的黄油 Sweet Butter 最短路 寻找一个点使得所有点到它的距离之和最小
P1828 香甜的黄油 Sweet Butter 闲来无事 写了三种最短路(那个Floyed是不过的) 题目描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1 ...
- POJ 1511 最短路spfa
题很简单 就是有向图中求给出的源点到其余所有点的最短路的和与其余所有点到源点的最短路之和 一开始以为dij对于正权图的单源最短路是最快的 写了一发邻接表的dij 结果超时 把所有的cin改成scanf ...
- Wormholes 最短路判断有无负权值
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- 详解zkw算法解决最小费用流问题
网络流的一些基本概念 很多同学建立过网络流模型做题目, 也学过了各种算法, 但是对于基本的概念反而说不清楚. 虽然不同的模型在具体叫法上可能不相同, 但是不同叫法对应的思想是一致的. 下面的讨论力求规 ...
- Bellman-Ford 求含负权最短路
该算法详解请看 https://www.cnblogs.com/tanky_woo/archive/2011/01/17/1937728.html 单源最短路 当图中存在负权边时 迪杰斯特拉就 ...
- 最短路之SPFA算法
部分来自:http://blog.csdn.net/juststeps/article/details/8772755 求最短路径的算法有许多种,除了排序外,恐怕是OI界中解决同一类问题算法最多的了. ...
- 关于dijkstra的优化 及 多源最短路
先来看这样一道题目 给你N个点,M条双向边,要求求出1号点到其他所有点的距离.其中 2 <= N <= 1e5, 1 <=M <= 1e6. 对于这样的一道题目 我们当然不可 ...
随机推荐
- spark知识点_datasources
来自官网DataFrames.DataSets.SQL,即sparkSQL模块. 通过dataframe接口,sparkSQL支持多种数据源的操作.可以把dataframe注册为临时视图,也可以通过关 ...
- MySQL 标识符到底区分大小写么——官方文档告诉你
最近在阿里云服务器上部署一个自己写的小 demo 时遇到一点问题,查看 Tomcat 日志后定位到问题出现在与数据库服务器交互的地方,执行 SQL 语句时会返回 指定列.指定名 不存在的错误.多方查证 ...
- 跨域的几种方式以及call(),apply() bind()方法的作用和区别
jsonp: jsonp 全称是JSON with Padding,是为了解决跨域请求资源而产生的解决方案,是一种依靠开发人员创造出的一种非官方跨域数据交互协议. 一个是描述信息的格式,一个是信息传递 ...
- 【Java基础】Java11 新特性
Java11 新特性 新增字符串处理方法 新增方法: 判断字符串是否为空白 " ".isBlank(); // true 去除首尾空白 " Javastack " ...
- Java JVM——9.方法区
前言 方法区是运行时数据区的最后一个部分: 从线程共享与否的角度来看: 大家可能在这里有些疑惑,方法区和元空间的关系到底是怎样的?请往下看,下面会为大家解惑. 栈.堆.方法区的交互关系 下面就涉及了对 ...
- 【LeetCode】数组排序题 Array_Sorts
数组排序 Array_Sorts LeetCode 数组排序题 88. 合并两个有序数组 合并两个有序数组 难度简单 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nu ...
- linux搭建简单samba服务器
1.安装需要的软体 yum install -y samba samba-client samba-common 2.创建samba需要的本地用户,创建samba服务使用的目录 Linux系统文件的读 ...
- Redis 实战 —— 01. Redis 数据结构简介
一些数据库和缓存服务器的特性和功能 P4 名称 类型 数据存储选项 查询类型 附加功能 Redis 使用内存存储(in-memory)的非关系数据库 字符串.列表.哈希表.集合.有序集合 每种数据类型 ...
- JVM 判断对象已死,实践验证GC回收
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 提升自身价值有多重要? 经过了风风雨雨,看过了男男女女.时间经过的岁月就没有永恒不变 ...
- 前端面试准备笔记之JavaScript(02)
01. this的典型应用场景 this在各个场景中取什么值,是在函数执行的时候确认的,不是在定义的时候确认的. 普通函数执行 返回window function fn1() { console.lo ...