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 动归的更多相关文章

  1. OpenJudge/Poj 1083 Moving Tables

    1.链接地址: http://poj.org/problem?id=1083 http://bailian.openjudge.cn/practice/1083/ 2.题目: 总时间限制: 1000m ...

  2. POJ 1083 Moving Tables 思路 难度:0

    http://poj.org/problem?id=1083 这道题题意是有若干段线段,每次要求线段不重叠地取,问最少取多少次. 因为这些线段都是必须取的,所以需要让空隙最小 思路: 循环直到线段全部 ...

  3. POJ 1083 Moving Tables

    题意:一个建筑物里有400个房间,房间都在一层里,在一个走廊的两侧,如图,现在要搬n张桌子,告诉你每张桌子是从哪个屋搬到哪个屋,搬桌子的线路之间不可以有重叠,问最少搬几次. 解法:贪心.一开始觉得只要 ...

  4. poj 1083 Moving Tables_dp

    题意:给你n个凳子,接着告诉你一个凳子从a房间到b房间,运输时间为10分钟,走廊很窄能通过一张凳子,当然不堵塞的话能同时扮凳子,问最小花费多少时间 因为数据很小就直接用数组统计了,a,b如果是奇数的话 ...

  5. 动态规划 is beginning。。。。。。。。。

    感觉动态规划非常模糊,怎么办呢??? 狂刷题吧!! !! ! !!! ! !!! !! ! ! ! .!! ..!.! PKU  PPt 动规解题的一般思路 1. 将原问题分解为子问题         ...

  6. 专题:DP杂题1

    A POJ 1018 Communication System B POJ 1050 To the Max C POJ 1083 Moving Tables D POJ 1125 Stockbroke ...

  7. DP 题集 2

    关于 DP 的一些题目 String painter 先区间 DP,\(dp[l][r]\) 表示把一个空串涂成 \(t[l,r]\) 这个子串的最小花费.再考虑 \(s\) 字符串,\(f[i]\) ...

  8. poj 1324 Holedox Moving

    poj 1324 Holedox Moving 题目地址: http://poj.org/problem?id=1324 题意: 给出一个矩阵中,一条贪吃蛇,占据L长度的格子, 另外有些格子是石头, ...

  9. POJ 1324 Holedox Moving (状压BFS)

    POJ 1324 Holedox Moving (状压BFS) Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 18091 Acc ...

随机推荐

  1. autosar

    AUTOSAR – RTE(1)基本概念 1. RTE概述 The Run-Time Environment (RTE) is at the heart of the AUTOSAR ECU arch ...

  2. layui框架下的摸索与学习

    一.table表格内的查询  1.单个条件查询: 主要代码: <%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/1/14 ...

  3. 洛谷P5087 数学

    DP. 设f[i][j]为前j个数中选i个数的所有组合的分数之和 决策: 不选这个数,得分为f[i][j - 1] 选这个数,得分为f[i - 1][j - 1] * a[j] 可以得到状态转移方程为 ...

  4. ASP.NET-缓存基本知识点

    asp.net cache是一种缓存技术,然而,我们在asp.net程序中还可以使用其他的缓存技术,这些不同的缓存也各有所长.由于asp.net cache不能提供对外访问能力,因此,它不可能取代以m ...

  5. Qt5.7新特性

    简述 Qt5.7发布了,新特性如下. 简述 新特性 C11 Support Required from the compiler New Features within existing module ...

  6. 15个开发者最亲睐的Andr​​oid代码编辑器

    如果你希望你的Android设备,如智能手机和平板电脑,在任何时间和任何地方都能够编写代码,那么,不妨看看下面我将介绍的15款Android代码编辑器,它们必将成为你的理想工具. 1.Deuter I ...

  7. Android开发学习之事件处理和Button具体解释

    Android的事件处理机制:  1.基于监听器的事件处理 --- 组件绑定特定的事件监听器   --- 重点   2.基于回调的事件处理   --- 主要做法是重写Android组件特定的回调函数, ...

  8. HDU 5310 Souvenir

    Souvenir  Accepts: 901  Submissions: 2743  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 262 ...

  9. Java类和对象11

    首先,编写一个类ChongZai,该类中有3个重载的方法void print():其次,再编写一个主类来测试ChongZai类的功能. public class ChongZai { public v ...

  10. 关于网易云音乐爬虫的api接口?

    抓包能力有限,分析了一下网易云音乐的一些api接口,但是关于它很多post请求都是加了密,没有弄太明白.之前在知乎看到过一个豆瓣工程师写的教程,但是被投诉删掉了,请问有网友fork了的吗?因为我觉得他 ...