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. CodeForces - 1003D

    Polycarp has nn coins, the value of the ii-th coin is aiai. It is guaranteed that all the values are ...

  2. 学习webpack

    前言 webpack前端工程中扮演的角色越来越重要,它也是前端工程化很重要的一环.本文将和大家一起按照项目流程学习使用wbepack,妈妈再也不用担心我不会使用webpack,哪里不会看哪里.这是一个 ...

  3. mac终端配色

    1. 终端输入 ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)" 2. brew installxz ...

  4. jQuery实现简单前端搜索功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 一键切图 PS 动作 【收藏】

    使用方法 一键切图动作.zip 1. 下载动作 2. 打开PS 动作 窗口,导入动作 3. 选中图层后 点击 F2 一键切图 详情看原文链接 原文链接

  6. TCP 传输控制协议(转)

    开头先说几个协议: IP:网际协议 TCP:传输控制协议 Http:超文本传输协议 AMQP:高级消息队列协议 一:TCP是什么? TCP(Transmission Control Protocol ...

  7. python实战===python程序打包成exe

    推荐PyInstaller项目www.pyinstaller.org   安装方法: 先跑pip install pywin32再跑pip install pyinstaller即可 可用一句命令打包 ...

  8. oracle日期格式转换 to_date()

    与date操作关系最大的就是两个转换函数:to_date(),to_char()       to_date() 作用将字符类型按一定格式转化为日期类型:       具体用法:to_date(''2 ...

  9. alias命令别名

    笔者在看<鸟哥私房菜>时,突然看到这个命令,之前未接触过,故简单记录学习下,具体的大家可参见man手册.功能说明:设置指令的别名.语 法:alias[别名]=[指令名称]参 数 :若不加任 ...

  10. python 爬图

    利用bs库进行爬取,在下载html时,使用代理user_agent来下载,并且下载次数是2次,当第一次下载失败后,并且http状态码是500-600之间,然后会重新下载一次 soup = Beauti ...