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. 题解 P3200 【[HNOI2009]有趣的数列】

    说起来这是今天第三道卡特兰数了... 楼上的几篇题解好像都是直接看出这是卡特兰数,所以我就写一下为什么这道题可以用卡特兰数吧. 考察这样相邻的两项:\(a_{2i-1}\)与\(a_{2i}\),根据 ...

  2. Login.hbm.xml

    <?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLI ...

  3. C++容器(二):关联容器简介

    关联容器(associative container)与顺序容器的本质区别在于:关联容器通过键(Key)存储和读取元素,而顺序容器则通过元素在容器中的位置顺序存储和访问元素.虽然,关联容器的大部分行为 ...

  4. openssh 升级到7.5p1

    1. 参照: http://www.cnblogs.com/xiegj/p/5669800.html http://blog.csdn.net/u011080082/article/details/5 ...

  5. less14 颜色函数2

    less div{ // hue()色相值 z-index: hue(hsl(90,100%,50%)); //90 ////saturation()饱和度 z-index: saturation(h ...

  6. bzoj2438: [中山市选2011]杀人游戏(强联通+特判)

    2438: [中山市选2011]杀人游戏 题目:传送门 简要题意: 给出n个点,m条有向边,进行最少的访问并且可以便利(n-1)个点,求这个方案成功的概率 题解: 一道非常好的题目! 题目要知道最大的 ...

  7. Android--ViewPager-Fragment

    package com.cnn.viewpager02; import java.util.ArrayList; import java.util.List; import android.os.Bu ...

  8. Linux 下段错误 core文件

    什么是core dump? core的意思是内存,dump的意思是扔出来,堆出来:当一个程序奔溃时,在进程当前工作目录的core文件中复制了该进程的存储图像.core文件仅仅是一个内存映像(同时加上调 ...

  9. 把枚举类型绑定到datasource

    /// <summary> ///ProjectPriority 的摘要说明 /// </summary> public enum ProjectPriority { 极低 = ...

  10. SSD-tensorflow-2 evaluation

    测试就是用voc2007的test set来测试已经训练好的checkpoint的mAP,github上提供了三个已经训练好的model的checkpoint checkpoint 里面已有的300_ ...