第九届河南理工大学算法程序设计大赛 正式赛L:最优规划(最小生成树)
单测试点时限: 1.0 秒
内存限制: 512 MB
有很多城市之间已经建立了路径,但是有些城市之间没有路径联通。为了联通所有的城市,现在需要添加一些路径,为了节约,需要满足添加总路径是最短的。
输入
第一行 333 个整数 n,m,sn, m, sn,m,s, 分别表示城市的数量、已经存在的路的数量、可修的路的数量。
之后的 mmm 行,每行 333 个整数 x,y,dx, y, dx,y,d,表示点 xxx 到点 yyy 有一条长度为 ddd 的已经存在的路径。
之后的 sss 行,每行 333 个整数 x,y,dx, y, dx,y,d,表示点 xxx 到点 yyy 有一条长度为 ddd 的可修的路径。
0<n,m,s,d≤1050<n,m,s,d≤10^50<n,m,s,d≤105 。
输出
输出一个整数表示需要添加的最短的路径长度。
若果无论如何也无法使得所有的城市联通,输出 Concubines can't do it.
。
样例
input
5 3 2
1 2 1
1 3 2
1 4 3
2 3 4
2 5 5
output
5
Solve
最小生成树裸题,前mmm条边的边权变成000,后sss条边的边权不变,进行MST
Code
#include<bits/stdc++.h>
#define ll long long
#define ms(a,b) memset(a,b,sizeof(a))
const int maxn=1e6+10;
const int maxm=1e3+10;
const int inf=(1<<30);
const ll INF=(1LL*1<<60);
using namespace std;
int f[maxn];
struct Edge
{
int u,v,w;
}edge[maxn];
int tol;
void add(int u,int v,int w)
{
edge[tol].u=u;
edge[tol].v=v;
edge[tol++].w=w;
}
bool cmp(Edge a,Edge b)
{
return a.w<b.w;
}
int Find(int x)
{
if(f[x]==x)
return x;
else
return f[x]=Find(f[x]);
}
ll kru(int n)
{
for(int i=0;i<=n;i++)
f[i]=i;
sort(edge,edge+tol,cmp);
int cnt=0;
ll ans=0;
for(int i=0;i<tol;i++)
{
int u=edge[i].u;
int v=edge[i].v;
int w=edge[i].w;
int tOne=Find(u);
int tTwo=Find(v);
if(tOne!=tTwo)
{
ans+=1LL*w;
f[tOne]=tTwo;
cnt++;
}
}
if(cnt<n-1)
return -1;
else
return ans;
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);
int n,m,s;
cin>>n>>m>>s;
int res=0;
for(int i=0;i<m;i++)
{
int x,y,d;
cin>>x>>y>>d;
res=max(max(x,y),res);
add(x,y,0);
add(y,x,0);
}
int a1=kru(m);
for(int i=0;i<s;i++)
{
int x,y,d;
cin>>x>>y>>d;
add(x,y,d);
add(y,x,d);
}
if(kru(n)==-1)
cout<<"Concubines can't do it."<<endl;
else
cout<<1LL*kru(n)<<endl;
return 0;
}
第九届河南理工大学算法程序设计大赛 正式赛L:最优规划(最小生成树)的更多相关文章
- 『NYIST』第九届河南省ACM竞赛队伍选拔赛[正式赛二]-最小内积(第八届北京师范大学程序设计竞赛决赛)
H. 最小内积 Time Limit: 1000ms Memory ...
- 『NYIST』第九届河南省ACM竞赛队伍选拔赛[正式赛二]- Nearly Lucky Number(Codeforces Beta Round #84 (Div. 2 Only)A. Nearly)
A. Nearly Lucky Number time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 『NYIST』第九届河南省ACM竞赛队伍选拔赛[正式赛二]--Codeforces -35D. Animals
D. Animals time limit per test 2 seconds memory limit per test 64 megabytes input input.txt output o ...
- “浪潮杯”第九届山东省ACM大学生程序设计竞赛重现赛 C-Cities
题目描述:There are n cities in Byteland, and the ith city has a value ai. The cost of building a bidirec ...
- Contest - 第10届“新秀杯”ACM程序设计大赛网络资格赛 赛后信息(晋级名单·正式版)
2014_acm_fresh_0057 刘畅 20131620 2014_acm_fresh_0099 汪哲 20132185 2014_acm_fresh_0086 陈顺 2014111776 20 ...
- 第十六届浙江大学宁波理工学院程序设计大赛 D 雷顿女士与分队hard version(dp)
题意 链接:https://ac.nowcoder.com/acm/contest/2995/D来源:牛客网 卡特莉接到来自某程序设计竞赛集训队的邀请,来为他们进行分队规划. 现在集训队共有n名选手, ...
- Contest - 第10届“新秀杯”ACM程序设计大赛现场热身赛 赛后信息(题解)
Problem Id Title Problem A A+B Problem B 统计字数 Problem C 生日计算 Problem D 冬瓜的寒假之旅 Problem A(略 ...
- Contest - 第10届“新秀杯”ACM程序设计大赛网络预选赛 赛后信息(晋级名单)
经过比赛结果以及综合评定,以下42名同学暂定出现.下为出现名单(打*为 友情参赛 或为 有重大作弊嫌疑的选手). 在即日24时之前,若有异议,仍可申诉,申诉邮箱:desgard_duan@foxmai ...
- 谁才是最强战舰!-From 南京理工大学第八届程序设计大赛(校外镜像),博弈~~
谁才是最强战舰! Time Limit: 1000MS Memory Limit: 65536KB Description 依阿华来到镇守府的第一件事情,就是找大和solo!然而这并不是什么好消息,说 ...
随机推荐
- c#GridView
分页: 1.先把属性AllowPaging设置为true, 2.pagesize为每一页的行数,PageSize="15". 3.OnPageIndexChanging=" ...
- ORACLE 服务器验证
位于$ORACLE_HOME/network/admin/sqlnet.oraSQLNET.AUTHENTICATION_SERVICES=none|all|ntsnone:关闭操作系统认证,只能密码 ...
- RecyclerView实现侧滑删除、置顶、滑动
1.首先在build.gradle里添加 compile 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.2.1' 2.设置recyclerView的item布 ...
- ping (网络诊断工具)
Ping是Windows.Unix和Lnix系统下的一个命令,ping也属于一个通信协议,是TCP/IP协议的一部分,利用Ping命令可以检查网络是否连通,可以很好地帮助我们分析和判定网络故障.应用格 ...
- RAC(Reactive Cocoa)常见的类
导入ReactiveCocoa框架 在终端,进入Reactive Cocoa文件下 创建podfile 打开该文件 并配置 use_frameworks! pod 'ReactiveCocoa', ' ...
- Oracle 学习PL/SQL
先上一张实用的图:用于转义字符的. SQL> select chr(42) ||'is what?' from dual; CHR(42)||---------*is what? 想转义哪个就转 ...
- react-native安卓运行报错:The number of method references in a .dex file cannot exceed 64K.
错误原因:App里面方法数超过64K解决方法:在android/app/build.gradle中添加implementation 'com.android.support:multidex:1.0. ...
- 使用buffered流结合byte数组,读入文件中的内容,包括中文字符
package com.itcast.demo05.Buffered;import java.io.BufferedInputStream;import java.io.FileInputStream ...
- spring中JDBCTemplate的简单应用
package cn.itcast.datasource.jdbctemplate;import cn.itcast.utils.JDBCUtils;import org.springframewor ...
- windows下更换MySql数据库数据文件夹位置
详细解决地址 ,感谢博主 :https://blog.csdn.net/u010953266/article/details/56499361 概述 由于更换硬盘,系统重新安装了一遍,原来的mysq ...