题目描述

温迪有一个国家,他想建立一支军队来保护他的国家。他收留了N个女孩和M个男孩,想把她们收留成他的士兵。征兵无特权,必须交纳一万元。女孩和男孩之间有一些关系,温迪可以利用这些关系来降低他的成本。如果X女孩和Y男孩有D关系,并且其中一个已经被收集,Windy可以用10000-D人民币收集另一个。现在考虑到男孩和女孩之间的所有关系,你的任务是找到风必须支付的最少的钱。注意,收集一个士兵时只能使用一个关系。Input

输入的第一行是测试用例的数量。 每个测试用例的第一行包含三个整数,n、m和r。 然后R行,每个包含三个整数Xi,Yi和DI。 每个测试用例前都有一个空白行。

1 ≤ NM ≤ 10000
0 ≤ R ≤ 50,000
0 ≤ xi < N
0 ≤ yi < M
0 < di < 10000

Output

For each test case output the answer in a single line.

Sample Input

2

5 5 8
4 3 6831
1 3 4583
0 0 6592
0 1 3063
3 3 4975
1 3 2049
4 2 2104
2 2 781 5 5 10
2 4 9820
3 2 6236
3 1 8864
2 4 8326
2 0 5156
2 0 1463
4 1 2439
0 4 4373
3 4 8889
2 4 3133

Sample Output

71071
54223
代码如下,参考挑战程序设计竞赛
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
#define ull unsigned long long
#define ll long long
const int maxn=5e4+;
int par[maxn];
int rank1[maxn],n,m,r; void init(int n) //初始化
{
for(int i=;i<n;i++)
{
par[i]=i;
rank1[i]=;
}
}
int find(int x)
{
if(par[x]==x)
{
return x;
}
else
{
return par[x]=find(par[x]);
}
}
void unite(int x,int y)
{
x=find(x);
y=find(y);
if(x==y)
return ;
if(rank1[x]<rank1[y])
{
par[x]=y;
}
else
{
par[y]=x;
}
if(rank1[x]==rank1[y])
rank1[x]++;
}
struct edge{
int x,y,cost;
}e[];
bool cmp(const edge e1,const edge e2)
{
return e1.cost<e2.cost;
}
ll kruskal()
{
ll res=;
sort(e+,e+r+,cmp);
for(int i=;i<=r;i++)
{
edge e1=e[i];
if(find(e1.x)!=find(e1.y))
{
unite(e1.x,e1.y);
res=res+e1.cost;
}
}
return res;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&r);
init(n+m);
for(int i=;i<=r;i++)
{
scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].cost);
e[i].cost=-e[i].cost;
e[i].y=e[i].y+n;
}
printf("%lld\n",10000LL*(n+m)+kruskal());
}
return ;
}

POJ3723(最小生成树,负权)的更多相关文章

  1. python数据结构与算法——图的最短路径(Bellman-Ford算法)解决负权边

    # Bellman-Ford核心算法 # 对于一个包含n个顶点,m条边的图, 计算源点到任意点的最短距离 # 循环n-1轮,每轮对m条边进行一次松弛操作 # 定理: # 在一个含有n个顶点的图中,任意 ...

  2. poj 3259 Wormholes 判断负权值回路

    Wormholes Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u   Java ...

  3. poj3259 bellman——ford Wormholes解绝负权问题

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35103   Accepted: 12805 Descr ...

  4. POJ 3259 Wormholes 虫洞(负权最短路,负环)

    题意: 给一个混合图,求判断是否有负环的存在,若有,输出YES,否则NO.有重边. 思路: 这是spfa的功能范围.一个点入队列超过n次就是有负环了.因为是混合图,所以当你跑一次spfa时发现没有负环 ...

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

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

  6. [ACM] POJ 3259 Wormholes (bellman-ford最短路径,推断是否存在负权回路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29971   Accepted: 10844 Descr ...

  7. dij算法为什么不能处理负权,以及dij算法变种

    对于上面那张图,是可以用dij算法求解出正确答案,但那只是巧合而已. 我们再看看下面这张图. dist[4] 是不会被正确计算的. 因为dij算法认为从队列出来的点,(假设为u)肯定是已经求出最短路的 ...

  8. POJ 3259 Wormholes Bellman_ford负权回路

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

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

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

  10. 图之单源Dijkstra算法、带负权值最短路径算法

    1.图类基本组成 存储在邻接表中的基本项 /** * Represents an edge in the graph * */ class Edge implements Comparable< ...

随机推荐

  1. codewars--js--Two Joggers--求最小公倍数、最大公约数

    问题描述: Two Joggers Description Bob and Charles are meeting for their weekly jogging tour. They both s ...

  2. MySQL手工注入学习-1

    MySQL手工注入学习 SQLi-labs 手工注入学习 以下是通过SLQi-labs平台的部分简单例题的手工注入过程 Less-1:union联合查询注入 页面提示:Please input the ...

  3. CentOS安装python3环境

    CentOS7.4安装python3环境 (Python 3.8.1) (stable version, Dec.18, 2019) # .从官网下载Python - Dec. , [stable v ...

  4. Centos7之firewall配置命令

    firewalld的基本使用 查看状态:systemctl status firewalld 启动:systemctl start firewalld 停止:systemctl stop firewa ...

  5. 软件测试常见术语(英->汉)收藏好随时备用!

    Defect 缺陷Defect Rate 缺陷率Verification & Validation 验证和确认Failure 故障White-box Testing 白盒测试Black-box ...

  6. Premiere Pro CC2018安装教程

    Premiere Pro CC2018安装教程 下载安装包:去官网下载或者百度PR2018的安装包 解压安装包后,找到Set-up.exe,右键,打开 安装的时候我们需要注册一个账号,点击“获取Ado ...

  7. Blazor初体验之寻找存储client-side jwt token的方法

    https://www.cnblogs.com/chen8854/p/securing-your-blazor-apps-authentication-with-clientside-blazor-u ...

  8. day 17 初始递归

    递归函数 了解什么是递归 : 在函数中调用自身函数 最大递归深度默认是997/998 —— 是python从内存角度出发做的限制 能看懂递归 能知道递归的应用场景 初识递归 —— 算法 —— 二分查找 ...

  9. 如何知道一个路由器的 BSSID ?

    使用 Mac 连接上这个路由器,然后使用 option 按 wifi 按钮,可以在详情页里找到. 有些路由中继的设置需要使用 BSSID ,比如 pandorabox openwrt

  10. CSRF防护

    CSRF防护 (待完善...)