Base StationTime
limit:5000/2000 MS (java/others) Memory limit:65768/32768 K (java/others)
Total submission (s): 1983 Accepted Submission (s): 838
Problem Descriptiona famous mobile communication company are planning to build a new set of base stations. According to the previous investigation, N places is chosen as the possible new locations to build those new stations. However, the condition of each position varies much, so the costs to built a station at different places is different. The cost to build a new station at the ith Place is Pi (1<=i<=n).
When complete building, the places which both has stations can communicate with each of the other.
Besides, according to the marketing department, the company had received m requirements. The ith requirement is represented by three integers Ai, Bi and Ci, which means if place Ai and Bi can communicate with EA Ch Other, the company'll get Ci profit.
Now, the company wants to maximize the profits, so maybe just part of the possible locations would be chosen to build new S Tations. The boss wants to know the maximum profits.
Inputmultiple test Cases (no more than), for each test case:
The first line has a integers n (0<n<=5000) and M (0<m<=50000).
The second line have n integers, P1 through Pn, describes the cost of each location.
Next m line contains three integers, Ai, Bi and Ci, describes the ith requirement.
Outputone integer, the maximum profit of the company.
Sample Input
5 51 2 3 4 51 2 32 3 41 3 31 4 24 5 3
Sample Output
4
Authorliulibo
Source2011 multi-university Training Contest 5-host by BNU
Recommendlcy | We have carefully selected several similar problems for you:3657 1565 3491 3887 3889
Test instructions: There are n places for the construction of the base station, the construction of each base station has a cost p, there is a M user group, the user group I user base station AI and BI to communicate, the company profit CI, the company has a choice of building base station, ask the maximum net profit is how much. Net Profit = Total revenue-Total cost.
Idea: First analyze the decision-making factors in the topic. After satisfying the group I user base, the benefit can be obtained, however, it is necessary to meet the requirements of the first user group: To set up the relay station AI and the broker Bi, and to spend the corresponding costs. Mindful of this so-called necessary condition, one can think of the nature of a closed graph. The analysis shows that the subject is a special case of the maximum weight closed graph. Abstract it into such a graph model: each user group I as a node to the corresponding relay station AI and the broker bi connected to the edge.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 55005#define MAXM 555005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FREE (i,a,b) for (i = A; I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A; i > b; i--) #define MEM (T, v) memset ((t), V, si Zeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n") typedef long Long ll;using Nam Espace std;struct edge{int u,v,cap,next;} edge[Maxm];int head[maxn],cur[maxn],level[maxn];int num,n,m;void init () {num=0; MEM (head,-1);} void Addedge (int u,int v,int W) {edge[num].u=u; edge[num].v=v; edge[num].cap=w; edge[num].next=head[u]; head[u]=num++; Edge[num].u=v; Edge[num].v=u; Edge[num].cap=0; EDGE[NUM].NEXT=HEAD[V]; head[v]=num++;} BOOL BFs (int s,int t) {mem (level,-1); queue<int>q; level[s]=0; Q.push (s); while (! Q.empty ()) {int U=q.front (); Q.pop (); for (int i=head[u];i+1;i=edge[i].next) {int v=edge[i].v; if (edge[i].cap>0&&level[v]==-1) {level[v]=level[u]+1; Q.push (v); }}} return level[t]!=-1;} int dfs (int u,int t,int f) {if (u==t) return F; for (int &i=cur[u];i+1;i=edge[i].next) {int v=edge[i].v; if (edge[i].cap>0&&level[v]==level[u]+1) {int D=dfs (v,t,min (F,edge[i].cap)); if (d>0) { Edge[i].cap-=d; Edge[i^1].cap+=d; return D; }}} return 0;} int dinic (int s,int t,int nodenum) {int flow=0; while (BFS (s,t)) {for (int i=0;i<nodenum+1;i++) cur[i]=head[i]; int F; while ((F=dfs (S,t,inf)) >0) flow+=f; } return flow;} int main () {//Freopen ("C:/users/asus1/desktop/in.txt", "R", stdin); int i,j,u,v,w; while (~SFF (n,m)) {int sum=0; Init (); for (i=1;i<=n;i++) {scanf ("%d", &w); Addedge (M+I,N+M+1,W); } for (i=1;i<=m;i++) {sfff (u,v,w); Sum+=w; Addedge (0,I,W); Addedge (I,u+m,inf); Addedge (I,v+m,inf); } printf ("%d\n", Sum-dinic (0,n+m+1,n+m+2)); } return 0;}
Base Station (HDU 3879 maximum power closure)