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 ...
随机推荐
- 关于vue事件监听的一个问题
由于新工作需要用vue,所以最近接触最多的也是vue,因为之前一直在用react,所以对于vue上手还是很快的.我也尽量找一些他们两个的异同点,除了多了一些辅助用的方法以外,最大的不同应该是对于组件间 ...
- Java基础学习总结(4)——对象转型
一.对象转型介绍 对象转型分为两种:一种叫向上转型(父类对象的引用或者叫基类对象的引用指向子类对象,这就是向上转型),另一种叫向下转型.转型的意思是:如把float类型转成int类型,把double类 ...
- ASP.NET-让html代码输出为字符串
HttpUtility.HtmlEncode这个函数可以让s变成没有攻击的代码,可以提升网站的安全性 public string Index(string s = "suepr name i ...
- 国庆 day 1 下午
一道图论好题(graph) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK有一张无向图G={V,E},这张无向图有n个点m条边组成.并且这是一张带权图, ...
- Hibernate_9_Person和IdCard实例_一对一关系:基于主键
1)建立Person类:(与8同样) 2)建立IdCard类:(与8同样) 3)建立持久化类: 1>保存方法(与8同样) 2>获取方法(与8同样) 3>删除方法(与8同样) ...
- Java JNI 入门篇——传递数组与修改数组
这里不在重复JavaJNI 的开发过程了,不熟悉的同学请参考:Java JNI HelloWorld 直接上主要代码: ArrayJNI.Java package com.example.jni; p ...
- pyspark.mllib.feature module
Feature Extraction Feature Extraction converts vague features in the raw data into concrete numbers ...
- java9新特性-21-java的动态编译器
1. 官方Feature 243: Java-Level JVM Compiler Interface 295: Ahead-of-Time Compilation 2. 产生背景 Oracle 一直 ...
- net实现压缩功能
public static class Compressor { public static byte[] Compress(byte[] data) { using (MemoryStream ou ...
- jsLittle源码封装对象
window.JSLi = ((function(){ var JSLi = function(selector) { return new JSLi.fn.init(selector); }; JS ...