日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

[POJ1155]TELE

發(fā)布時(shí)間:2025/3/14 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [POJ1155]TELE 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

[POJ1155]TELE

試題描述

A TV-network plans to broadcast an important football match. Their network of transmitters and users can be represented as a tree. The root of the tree is a transmitter that emits the football match, the leaves of the tree are the potential users and other vertices in the tree are relays (transmitters).?
The price of transmission of a signal from one transmitter to another or to the user is given. A price of the entire broadcast is the sum of prices of all individual signal transmissions.?
Every user is ready to pay a certain amount of money to watch the match and the TV-network then decides whether or not to provide the user with the signal.?
Write a program that will find the maximal number of users able to watch the match so that the TV-network's doesn't lose money from broadcasting the match.

輸入

The first line of the input file contains two integers N and M, 2 <= N <= 3000, 1 <= M <= N-1, the number of vertices in the tree and the number of potential users.?
The root of the tree is marked with the number 1, while other transmitters are numbered 2 to N-M and potential users are numbered N-M+1 to N.?
The following N-M lines contain data about the transmitters in the following form:?
K A1 C1 A2 C2 ... AK CK?
Means that a transmitter transmits the signal to K transmitters or users, every one of them described by the pair of numbers A and C, the transmitter or user's number and the cost of transmitting the signal to them.?
The last line contains the data about users, containing M integers representing respectively the price every one of them is willing to pay to watch the match.

輸出

The first and the only line of the output file should contain the maximal number of users described in the above text.

輸入示例

9 6 3 2 2 3 2 9 3 2 4 2 5 2 3 6 2 7 2 8 2 4 3 3 3 1 1

輸出示例

5

數(shù)據(jù)規(guī)模及約定

見“輸入”;另:過程中不會(huì)有超過 int 的值。

題解

樹形 dp(樹上背包)。

設(shè) f(i, j) 表示子樹 i 中選擇了 j 個(gè)葉子的最大獲利(若為負(fù)則 -f(i, j) 為最小虧損)。那么答案就是最大的 j,滿足 f(i, j) 非負(fù)。

考慮子樹 u,兒子上的信息肯定是最有子結(jié)構(gòu),所以先算出所有的 f(son, j),然后分別將一個(gè)個(gè)子樹的信息加入 f(i, j)(f(u, i+j) = max{ f(u, i) + f(son, j) - dist(i, son) | j > 0 , f(u, i) + f(son, j) | j = 0 })。

可以證明總轉(zhuǎn)移數(shù)是 O(n2) 級別的,詳見這里。

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <algorithm> using namespace std;const int BufferSize = 1 << 16; char buffer[BufferSize], *Head, *Tail; inline char Getchar() {if(Head == Tail) {int l = fread(buffer, 1, BufferSize, stdin);Tail = (Head = buffer) + l;}return *Head++; } int read() {int x = 0, f = 1; char c = Getchar();while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }return x * f; }#define maxn 3010 #define oo 2147483647int n, usr, m, head[maxn], nxt[maxn], to[maxn], dist[maxn], pay[maxn];void AddEdge(int a, int b, int c) {to[++m] = b; dist[m] = c; nxt[m] = head[a]; head[a] = m;return ; }int f[maxn][maxn], clea[maxn]; void dp(int u) {if(u > n - usr) {clea[u] = 1;f[u][0] = 0; f[u][1] = pay[u];return ;}f[u][0] = 0;for(int e = head[u]; e; e = nxt[e]) {dp(to[e]);for(int i = clea[u]; i >= 0; i--) if(f[u][i] < oo)for(int j = 0; j <= clea[to[e]]; j++) if(f[to[e]][j] < oo)f[u][i+j] = max(f[u][i+j], f[u][i] + f[to[e]][j] - (j ? dist[e] : 0));clea[u] += clea[to[e]];}return ; }int main() {n = read(); usr = read();for(int i = 1; i <= n - usr; i++) {int k = read();while(k--) {int u = read(), c = read();AddEdge(i, u, c);}}for(int i = n - usr + 1; i <= n; i++) pay[i] = read();for(int i = 1; i <= n; i++)for(int j = 0; j <= n; j++) f[i][j] = -oo;dp(1);for(int j = clea[1]; j; j--) if(f[1][j] >= 0) return printf("%d\n", j), 0;puts("0");return 0; }

?

轉(zhuǎn)載于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/7309030.html

總結(jié)

以上是生活随笔為你收集整理的[POJ1155]TELE的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。