Codeforces 806 D.Prishable Roads

题目大意:给出一张完全图,你需要选取其中的一些有向边,连成一个树形图,树形图中每个点的贡献是其到根节点路径上每一条边的边权最小值,现在你需要求出每一个点作为根得到的树形图的贡献之和最小值。

解题思路:不难发现,最终答案一定是一条链挂着一个菊花的形态,且一定存在一种最优解菊花和链相连的边是权值最小的边, 如果不是,那么最小的边在链上可以直接把其它边免费挂到它下面,更优,如果最小边在菊花上,那么菊花上的其它边接在最小的边下面形成新的菊花会更优。那么我们可以把所有边权都减去最小的边权 \(\min\) ,现在我们要最小话根到这条最小的边的这条链上的边权之和。可以归纳证明这条链从最小边到根的路径上除了第一条边和第二条边的边权递增,那么这些边的贡献就是边权,可以直接跑最短路,而对于起始的那两条边,分类讨论一下那条边权值更大用一个超级源建两种边即可。跑不加优化的 Dijstra的复杂度是 \(O(n^2)\)

code

/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf ((ll)(1e17))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T & x){
int ch = 0, f = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
#define int ll
const int N = 2005;
int a[N*N*2], b[N*N*2], head[N], nxt[N*N*2], cnt;
int w[N][N], dis[N], now[N], tag[N], vis[N], n, S;
inline void add(int x, int y, int z){
a[++cnt] = y, b[cnt] = z, nxt[cnt] = head[x], head[x] = cnt;
}
signed main(){
read(n), S = n + 1; int Minedge = inf;
for(int i = 1; i < n; i++)
for(int j = i + 1; j <= n; j++)
read(w[i][j]), w[j][i] = w[i][j], Minedge = Min(w[i][j], Minedge);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) if(i != j){
add(i, j, w[i][j] -= Minedge);
if(!w[i][j]) tag[i] = 1, add(S, i, 0);
}
memset(now, 0x3f, sizeof(now));
for(int i = 1; i <= n; i++) if(!tag[i]){
for(int j = 1; j <= n; j++)
if(!tag[j] && i != j) now[j] = Min(now[j], w[i][j] << 1); }
for(int i = 1; i <= n; i++) add(S, i, now[i]);
memset(dis, 0x3f, sizeof(dis)), dis[S] = 0;
for(int k = 1; k <= n; k++){
int mn = inf, u = 0;
for(int i = 1; i <= n + 1; i++)
if(!vis[i] && dis[i] < mn) mn = dis[i], u = i;
vis[u] = 1;
for(int p = head[u]; p; p = nxt[p])
if(dis[u] + b[p] < dis[a[p]]) dis[a[p]] = dis[u] + b[p];
}
for(int i = 1; i <= n; i++)
printf("%lld\n", dis[i] + (n - 1) * Minedge);
return 0;
}

Codeforces 806 D.Prishable Roads的更多相关文章

  1. Codeforces 806 D. Perishable Roads Dijkstra

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

  2. Codeforces 191C Fools and Roads(树链拆分)

    题目链接:Codeforces 191C Fools and Roads 题目大意:给定一个N节点的数.然后有M次操作,每次从u移动到v.问说每条边被移动过的次数. 解题思路:树链剖分维护边,用一个数 ...

  3. codeforces 711D D. Directed Roads(dfs)

    题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  4. Codeforces 711 D. Directed Roads (DFS判环)

    题目链接:http://codeforces.com/problemset/problem/711/D 给你一个n个节点n条边的有向图,可以把一条边反向,现在问有多少种方式可以使这个图没有环. 每个连 ...

  5. Codeforces 746 G. New Roads

    题目链接:http://codeforces.com/contest/746/problem/G mamaya,不知道YY了一个什么做法就这样过去了啊 2333 首先我显然可以随便构造出一棵树满足他所 ...

  6. codeforces 1141G Privatization of Roads in Treeland

    题目链接:http://codeforces.com/contest/1141/problem/G 题目大意: 给你一个无向连通图.每条边都有颜色,如果存在一个点的临边中有超过两条边颜色相同,这个点就 ...

  7. Codeforces 543.B Destroying Roads

    B. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. 【CodeForces】671 D. Roads in Yusland

    [题目]D. Roads in Yusland [题意]给定n个点的树,m条从下往上的链,每条链代价ci,求最少代价使得链覆盖所有边.n,m<=3*10^5,ci<=10^9,time=4 ...

  9. Codeforces 583 DIV2 Asphalting Roads 模拟

    原题链接:http://codeforces.com/problemset/problem/583/A 题意: 很迷很迷,表示没看懂..但是你看样例就秒懂了 题解: 照着样例模拟就好 代码: #inc ...

随机推荐

  1. ASP.Net Cache(缓存)—ASP.NET细枝末节(2)

    概述 1.意义 把数据放到Cache中,在指定的时间内,可以直接从Cache中获取,避免对数据库等的压力. 2.做法 设置: HttpRuntime.Cache.Insert(CacheKey, ob ...

  2. CodeForces 990C

    Description A bracket sequence is a string containing only characters "(" and ")" ...

  3. WordPress手机端插件——WPtouch

    戒微博之后,把更多的精力开始转投回网站上来:今天用nexus7访问@Bee君 的博客时,发现博客的界面与电脑上访问的界面不相同,顺藤摸瓜之后发现原来bee君使用的是WPtouch-pro插件来实现移动 ...

  4. django【ORM】model字段类型

    1.AutoField 一个自增的IntegerField,一般不直接使用,Django会自动给每张表添加一个自增的primary key. 2.BigIntegerField 64位整数, -922 ...

  5. 从LabVIEW到C++

    前言 最近一段时间一直没有更新,一方面是时间精力的问题(PS:懒癌犯了),另一方面是小黑大部分的时间都在学习C++相关知识,恶补了许多的知识(从大学C语言水平强制拔高了一段). 本文谈谈自己近期的一些 ...

  6. angular项目中使用angular-material2

    Step 1: Install Angular Material and Angular CDK npm install --save @angular/material @angular/cdk n ...

  7. ubuntu查看mysql版本的几种方法

    ubuntu查看mysql版本的几种方法 mysql 1:在终端下:mysql -V(大写) //代码 $ mysql -V mysql Ver 14.14 Distrib 5.5.46, for d ...

  8. java版云笔记(八)之关联映射

    Mybatis关联映射 通过数据库对象之间的关联关系,反映到到实体对象之间的引用. 加载多个表中的关联数据,封装到我们的实体对象中. 当业务对数据库进行关联查询. 关联 <association ...

  9. oracle相关命令收集-张

    orcle相关命令收集 1,用管理员登陆 /as sysdba:2, 更改用户密码 alter user name identified by password: alter user exptest ...

  10. Gitlab部署及汉化操作

    一.简介 GitLab是一个利用 Ruby on Rails 开发的开源应用程序,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目. GitLab拥有与Github类似的功能 ...