[CF773D]Perishable Roads

题目大意:

一个\(n(n\le2000)\)个点的完全图\(G\),定义\(d(x)\)为生成树上点\(x\)到根路径上的最小边权。问图\(G\)的生成树\(\sum d(x)\)最小是多少?

思路:

题解得到图的一些性质,然后就不难了。

源代码:

#include<cstdio>
#include<cctype>
#include<climits>
#include<algorithm>
using int64=long long;
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
constexpr int N=2001;
bool vis[N];
int w[N][N],d[N];
int main() {
const int n=getint();
int min=INT_MAX;
for(register int i=1;i<=n;i++) {
for(register int j=i+1;j<=n;j++) {
min=std::min(min,w[i][j]=w[j][i]=getint());
}
}
d[0]=INT_MAX;
for(register int i=1;i<=n;i++) {
d[i]=INT_MAX;
for(register int j=1;j<=n;j++) {
if(i==j) continue;
w[i][j]-=min;
d[i]=std::min(d[i],w[i][j]*2);
}
}
for(register int i=1;i<=n;i++) {
int k=0;
for(register int j=1;j<=n;j++) {
if(!vis[j]&&d[j]<d[k]) k=j;
}
vis[k]=true;
for(register int j=1;j<=n;j++) {
d[j]=std::min(d[j],d[k]+w[k][j]);
}
}
for(register int i=1;i<=n;i++) {
printf("%lld\n",(int64)min*(n-1)+d[i]);
}
return 0;
}

[CF773D]Perishable Roads的更多相关文章

  1. Codeforces 806 D. Perishable Roads Dijkstra

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF806D.html 题目传送门 - CF806D 题意 给定一个 n 个点的无向完全图,每一条边有一定的边权. ...

  2. Codeforces Round#412 Div.2

    A. Is it rated? 题面 Is it rated? Here it is. The Ultimate Question of Competitive Programming, Codefo ...

  3. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  4. Jungle Roads[HDU1301]

    Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  5. POJ1947 Rebuilding Roads[树形背包]

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11495   Accepted: 5276 ...

  6. Constructing Roads——F

    F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...

  7. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

  8. 【CodeForces 567E】President and Roads(最短路)

    Description Berland has n cities, the capital is located in city s, and the historic home town of th ...

  9. POJ 1947 Rebuilding Roads

    树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...

随机推荐

  1. struts2学习笔记(三)

    一. Struts2 的验证 1). 验证分为两种: > 声明式验证* >> 对哪个 Action 或 Model 的那个字段进行验证 >> 使用什么验证规则 >& ...

  2. Spring - IoC(9): @Resoure & @Autowired

    @Resource 和 @Autowired 都是用来装配依赖的,它们之间有些异同. @Resoure @Resource 是 JSR-250 规范的注解. @Resource 可以标注在字段.方法上 ...

  3. 【BZOJ2227】【ZJOI2011】看电影 [组合数][质因数分解]

    看电影 Time Limit: 10 Sec  Memory Limit: 259 MB[Submit][Status][Discuss] Description 到了难得的假期,小白班上组织大家去看 ...

  4. 【POJ 1719】 Shooting Contest (二分图匹配)

    题目链接 把每一列能射的两行和这一列连边,然后跑一边匈牙利就行了. #include <cstdio> #include <cstring> #include <algo ...

  5. 设置session过期时间

    1如下是登录注册和记住密码的功能: # -*- coding: utf-8 -*- def cms_login(request): if request.method == 'GET': return ...

  6. Oracle rman 全备份的一个小例子

    run{  allocate channel d1 type disk;  backup database format='/u01/backup/%T_%d_%s.bak';  sql 'alter ...

  7. Centos7/RHEL 7 配置静态路由

    如图: 业务地址:192.168.10.0/24    192.168.20.0/24管理地址:172.168.10.0/24    172.168.20.0/24 需求:每台主机配置两张网卡,分别连 ...

  8. CentOS7配置阿里云yum源和EPEL源

    配置阿里云yum源(参考:http://mirrors.aliyun.com/help/centos) 1.备份 [root@bogon ~]# cd /etc/yum.repos.d/ [root@ ...

  9. 正则表达式、re、常用模块

    阅读目录 正则表达式 字符 量词 . ^ $ * + ? { } 字符集[][^] 分组 ()与 或 |[^] 转义符 \ 贪婪匹配 re 总结 正则 re 常用模块 namedtuple deque ...

  10. 对数据访问层的重构(及重构中Perl的应用)

    以前上学的时候,听到“一个学生在毕业后刚刚开始编程的头几年中,写出的代码多半是垃圾”这样的说法,均不屑一顾.现在工作一年多了,越发感觉自己代码中疏漏处甚多,故近来常做亡羊补牢的重构之举.拿自己4个月前 ...