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. pip 出错

    pip 升级到10以上出错 ImportError: cannot import name 'main' 解决方法一: 降低pip的版本号 python -m pip install pip==9.0 ...

  2. 架构思想之CAP原理

    由于自己负责后端的设计已经有一段时间,对设计的一些思想和理论有一些理解,但最近被问到什么是CAP时,却一脸懵逼,下来后专门针对CAP架构思想进行了一些专题学习,在这里也将这个概念引入给大家,大家可以有 ...

  3. 【问题】解决python3不支持mysqldb

    Django框架使用的还是python2.x的MySQLdb,而python3.x使用的是pymysql,centos7上默认安装的python2.7,自己安装了python3.6的版本,在运行dja ...

  4. [Luogu]P3338 [ZJOI2014]力(FFT)

    题目描述 给出\(n\)个数\(q_i\),给出\(F_j\)的定义如下: \(F_j = \sum_{i<j}\frac{q_i q_j}{(i-j)^2 }-\sum_{i>j}\fr ...

  5. 【codeforces 65A】Harry Potter and Three Spells

    [题目链接]:http://codeforces.com/problemset/problem/65/A [题意] 你有3种魔法; 1.可以将a单位的石头变成b单位的铅 2.可以将c单位的铅变成d单位 ...

  6. Json学习总结(2)——Java 下的 JSON库性能比较:JSON.simple vs. GSON vs. Jackson vs. JSONP

    JSON已经成为当前服务器与WEB应用之间数据传输的公认标准,不过正如许多我们所习以为常的事情一样,你会觉得这是理所当然的便不再深入思考了.我们很少会去想用到的这些JSON库到底有什么不同,但事实上它 ...

  7. Linux入门基础(一)

    UNIX/Linux 本身是没有图形界面的,我们通常在 UNIX/Linux 发行版上看到的图形界面实际都只是运行在 Linux 系统之上的一套软件XFree86,现在则是 xorg(X.Org),而 ...

  8. Hive-jdbc获取sessionId

    在HiveStatement中有一个sessHandle: public class HiveStatement implements java.sql.Statement { ... private ...

  9. 转:app store 注册账号生成证书上传app完整的教程

    app store为开发者提供四种类型的申请: 个人ios开发者计划$99/年 公司ios开发者计划$99/年 企业ios开发者计划$299/年 高校ios开发者计划免费 在这里主要介绍一下公司ios ...

  10. [NOIP2015模拟10.27] 挑竹签 解题报告(拓扑排序)

    Description 挑竹签——小时候的游戏夏夜,早苗和诹访子在月光下玩起了挑竹签这一经典的游戏.挑竹签,就是在桌上摆上一把竹签,每次从最上层挑走一根竹签.如果动了其他的竹签,就要换对手来挑.在所有 ...