Codeforces 806 D.Prishable Roads
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的更多相关文章
- Codeforces 806 D. Perishable Roads Dijkstra
原文链接https://www.cnblogs.com/zhouzhendong/p/CF806D.html 题目传送门 - CF806D 题意 给定一个 n 个点的无向完全图,每一条边有一定的边权. ...
- Codeforces 191C Fools and Roads(树链拆分)
题目链接:Codeforces 191C Fools and Roads 题目大意:给定一个N节点的数.然后有M次操作,每次从u移动到v.问说每条边被移动过的次数. 解题思路:树链剖分维护边,用一个数 ...
- codeforces 711D D. Directed Roads(dfs)
题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 711 D. Directed Roads (DFS判环)
题目链接:http://codeforces.com/problemset/problem/711/D 给你一个n个节点n条边的有向图,可以把一条边反向,现在问有多少种方式可以使这个图没有环. 每个连 ...
- Codeforces 746 G. New Roads
题目链接:http://codeforces.com/contest/746/problem/G mamaya,不知道YY了一个什么做法就这样过去了啊 2333 首先我显然可以随便构造出一棵树满足他所 ...
- codeforces 1141G Privatization of Roads in Treeland
题目链接:http://codeforces.com/contest/1141/problem/G 题目大意: 给你一个无向连通图.每条边都有颜色,如果存在一个点的临边中有超过两条边颜色相同,这个点就 ...
- Codeforces 543.B Destroying Roads
B. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 【CodeForces】671 D. Roads in Yusland
[题目]D. Roads in Yusland [题意]给定n个点的树,m条从下往上的链,每条链代价ci,求最少代价使得链覆盖所有边.n,m<=3*10^5,ci<=10^9,time=4 ...
- Codeforces 583 DIV2 Asphalting Roads 模拟
原题链接:http://codeforces.com/problemset/problem/583/A 题意: 很迷很迷,表示没看懂..但是你看样例就秒懂了 题解: 照着样例模拟就好 代码: #inc ...
随机推荐
- 【CodeForces】671 C. Ultimate Weirdness of an Array
[题目]C. Ultimate Weirdness of an Array [题意]给定长度为n的正整数序列,定义一个序列的价值为max(gcd(ai,aj)),1<=i<j<=n, ...
- 【BZOJ】1468: Tree(POJ1741) 点分治
[题意]给定带边权树,求两点距离<=k的点对数.n<=40000. [算法]点分治 [题解]对于一个区域,选择其重心x作为根,则划分出来的每棵子树都是子区域,可以证明至多划分log n次( ...
- echarts初探:了解模块化
什么是echarts?这是官网:http://echarts.baidu.com/ 简单的说就是百度提供的一些画图表的库,用它你可以简便的画出一些你想要的图表效果. 虽然蛮好用的,但对于不知道模块化的 ...
- Python 生成随机数
import random x = int(input('Enter a number for x: ')) --随机数最小值y = int(input('Enter a number for y: ...
- Java network programming-guessing game
猜数字游戏 游戏的规则如下: 当客户端第一次连接到服务器端时,服务器端生产一个[0,50]之间的随机数字,然后客户端输入数字来猜该数字,每次客户端输入数字以后,发送给服务器端,服务器端判断该客户端发送 ...
- samba中的pdbedit用法
pdbedit用于在samba服务器中创建用户: 它的用法包括 pdbedit -a username:新建Samba账户. pdbedit -x username:删除Samba账户. pdbedi ...
- Ubuntu下安装Sublime Text3
1. 下载软件 Ctrl+Alt+T 调出命令窗口执行下面命令下载安装包: sudo add-apt-repository ppa:webupd8team/sublime-text-3 2. 更新软件 ...
- Shp上传至Oracle Spatial
1.下载shp2sdo,将shp文件拷贝至shp2sdo相同路径下,打开windows命令窗口,执行: shp2sdo shp文件名 表名 -i id -s 4326 -d 例如:shp2sdo ci ...
- rds 与mysql 进行主从同步
.rds上默认会有server-****,只需要配置从数据库: .从数据库的配置流程: .[mysqld] log-bin = mysql-bin-changelog #要和主库中的名字一样 rela ...
- POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))
题目链接:http://poj.org/problem?id=1511 题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9.问你从起点到各个点以及从各个 ...