POJ - 3847 Moving to Nuremberg 动归
POJ - 3847 Moving to Nuremberg
题意:一张无向有权图,包括边权和点权,求一点,使得到其他点的点权*边权之和最小
思路:
#pragma comment(linker, "/STACK:1000000000")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#define LL long long
#define INF 0xfffffffffffffff
#define IN freopen("in.txt","r",stdin)
#define OUT freopen("out.txt", "w", stdout)
#define MAXN 50005
using namespace std; struct Edge{
int to;
LL cost;
Edge(int to = , LL cost = ) :to(to), cost(cost){};
};
Edge G[MAXN << ];
int next[MAXN << ], head[MAXN << ];
int e;
void AddEdge(int from, int to, LL cost){
e++;
next[e] = head[from];
head[from] = e;
G[e].to = to;
G[e].cost = cost;
}
LL g[MAXN], f[MAXN], dis[MAXN], a[MAXN];
int cnt;
LL ans;
void dfs(int x, int y){
g[x] = ;
dis[x] = ;
for (int i = head[x]; i != ; i = next[i]){
Edge& e = G[i];
if (e.to == y) continue;
dfs(e.to, x);
dis[x] += dis[e.to] + 2LL * e.cost * g[e.to];
g[x] += g[e.to];
}
g[x] += a[x];
}
void work(int x, int y, LL res){
f[x] = res;
ans = min(f[x], ans);
for (int i = head[x]; i != ; i = next[i]){
Edge& e = G[i];
if (e.to == y) continue;
work(e.to, x, res + 2LL * e.cost * (cnt - 2LL * g[e.to]));
}
}
int main()
{
//IN;
int T;
scanf("%d", &T);
int n, m;
while (T--){
scanf("%d", &n);
int x, y;
LL z;
memset(dis, , sizeof(dis));
memset(head, , sizeof(head));
memset(a, , sizeof(a));
e = ;
for (int i = ; i < n; i++){
scanf("%d%d%I64d", &x, &y, &z);
AddEdge(x, y, z);
AddEdge(y, x, z);
}
scanf("%d", &m);
cnt = ;
for (int i = ; i <= m; i++){
scanf("%d%d", &x, &y);
a[x] = y;
cnt += y;
}
dfs(, );
ans = INF;
work(, , dis[]);
printf("%I64d\n", ans);
for (int i = ; i <= n; i++){
if (f[i] != ans) continue;
printf("%d ", i);
}
printf("\n"); }
return ;
}
POJ - 3847 Moving to Nuremberg 动归的更多相关文章
- OpenJudge/Poj 1083 Moving Tables
1.链接地址: http://poj.org/problem?id=1083 http://bailian.openjudge.cn/practice/1083/ 2.题目: 总时间限制: 1000m ...
- POJ 1083 Moving Tables 思路 难度:0
http://poj.org/problem?id=1083 这道题题意是有若干段线段,每次要求线段不重叠地取,问最少取多少次. 因为这些线段都是必须取的,所以需要让空隙最小 思路: 循环直到线段全部 ...
- POJ 1083 Moving Tables
题意:一个建筑物里有400个房间,房间都在一层里,在一个走廊的两侧,如图,现在要搬n张桌子,告诉你每张桌子是从哪个屋搬到哪个屋,搬桌子的线路之间不可以有重叠,问最少搬几次. 解法:贪心.一开始觉得只要 ...
- poj 1083 Moving Tables_dp
题意:给你n个凳子,接着告诉你一个凳子从a房间到b房间,运输时间为10分钟,走廊很窄能通过一张凳子,当然不堵塞的话能同时扮凳子,问最小花费多少时间 因为数据很小就直接用数组统计了,a,b如果是奇数的话 ...
- 动态规划 is beginning。。。。。。。。。
感觉动态规划非常模糊,怎么办呢??? 狂刷题吧!! !! ! !!! ! !!! !! ! ! ! .!! ..!.! PKU PPt 动规解题的一般思路 1. 将原问题分解为子问题 ...
- 专题:DP杂题1
A POJ 1018 Communication System B POJ 1050 To the Max C POJ 1083 Moving Tables D POJ 1125 Stockbroke ...
- DP 题集 2
关于 DP 的一些题目 String painter 先区间 DP,\(dp[l][r]\) 表示把一个空串涂成 \(t[l,r]\) 这个子串的最小花费.再考虑 \(s\) 字符串,\(f[i]\) ...
- poj 1324 Holedox Moving
poj 1324 Holedox Moving 题目地址: http://poj.org/problem?id=1324 题意: 给出一个矩阵中,一条贪吃蛇,占据L长度的格子, 另外有些格子是石头, ...
- POJ 1324 Holedox Moving (状压BFS)
POJ 1324 Holedox Moving (状压BFS) Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 18091 Acc ...
随机推荐
- 【jQuery02】点击标题面板显示内容
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- spring boot ---web应用开发-错误处理
一.错误的处理 方法一:Spring Boot 将所有的错误默认映射到/error, 实现ErrorController @Controller @RequestMapping(value = &qu ...
- [vue插件]基于vue2.x的电商图片放大镜插件
最近在撸一个电商网站,有一个需求是要像淘宝商品详情页那样,鼠标放在主图上,显示图片放大镜效果,找了一下貌似没有什么合适的vue插件,于是自己撸了一个,分享一下.小白第一次分享,各位大神莫见笑. vue ...
- Matlab 图像的邻域和块操作
图像的邻域操作是指输出图像的像素点取值,由输入图像的某个像素点及其邻域内的像素,通常像素点的邻域是一个远小于图像本身尺寸.形状规则的像素块,如2×2,3×3正方形.2×3矩形等,或者近似圆形的多边形. ...
- Cube Simulation zoj3429 模拟
Description Here's a cube whose size of its 3 dimensions are all infinite. Meanwhile, there're 6 pro ...
- thinkphp5项目--个人博客(三)
thinkphp5项目--个人博客(三) 项目地址 fry404006308/personalBlog: personalBloghttps://github.com/fry404006308/per ...
- spring-data-redis 使用过程中需要注意的地方
1.序列化问题 <!-- SDR默认采用的序列化策略有两种,一种是String的序列化策略,一种是JDK的序列化策略. StringRedisTemplate默认采用的是String的序列化策略 ...
- 虚拟主机TOMCAT配置
在tomcat中添加虚拟主机: 编辑"tomcat\conf\server.xml",在"<Engine></Engine>"元素中新加 ...
- PostgreSQL Replication之第二章 理解PostgreSQL的事务日志(2)
2.2 XLOG和复制 在本章中,您已经了解到PostgreSQL的事务日志已经对数据库做了所有的更改.事务日志本身被打包为易用的16MB段. 使用这种更改集来复制数据的想法是不牵强的.事实上,这是在 ...
- Mac or windows eclipse配置tomcat
1.选择window --> Preferences 首选项 2.选择server --> Runtime Environements --> Add 3.选择对应的tomcat版本 ...