日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[POJ1155]TELE

發布時間:2025/3/14 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [POJ1155]TELE 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[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

數據規模及約定

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

題解

樹形 dp(樹上背包)。

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

考慮子樹 u,兒子上的信息肯定是最有子結構,所以先算出所有的 f(son, j),然后分別將一個個子樹的信息加入 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 })。

可以證明總轉移數是 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; }

?

轉載于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/7309030.html

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。