River Problem HDU - 3947(公式建边)

River Problem

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 721    Accepted Submission(s): 282


node

Problem Description
The River of Bitland is now heavily polluted. To solve this problem, the King of Bitland decides to use some kinds of chemicals to make the river clean again.

The structure of the river contains n nodes and exactly n-1 edges between those nodes. It's just the same as all the rivers in this world: The edges are all unidirectional to represent water flows. There are source points, from which the water flows, and there is exactly one sink node, at which all parts of the river meet together and run into the sea. The water always flows from sources to sink, so from any nodes we can find a directed path that leads to the sink node. Note that the sink node is always labeled 1.

As you can see, some parts of the river are polluted, and we set a weight Wi for each edge to show how heavily polluted this edge is. We have m kinds of chemicals to clean the river. The i-th chemical can decrease the weight for all edges in the path from Ui to Vi by exactly 1. Moreover, we can use this kind of chemical for Li times, the cost for each time is Ci. Note that you can still use the chemical even if the weight of edges are 0, but the weight of that edge will not decrease this time.

When the weight of all edges are 0, the river is cleaned, please help us to clean the river with the least cost.
 

 

Input
The first line of the input is an integer T representing the number of test cases. The following T blocks each represents a test case.

The first line of each block contains a number n (2<=n<=150) representing the number of nodes. The following n-1 lines each contains 3 numbers U, V, and W, means there is a directed edge from U to V, and the pollution weight of this edge is W. (1<=U,V<=n, 0<=W<=20)

Then follows an number m (1<=m<=2000), representing the number of chemical kinds. The following m lines each contains 4 numbers Ui, Vi, Li and Ci (1<=Ui,Vi<=n, 1<=Li<=20, 1<=Ci<=1000), describing a kind of chemical, as described above. It is guaranteed that from Ui we can always find a directed path to Vi.
 

 

Output
First output "Case #k: ", where k is the case numbers, then follows a number indicating the least cost you are required to calculate, if the answer does not exist, output "-1" instead.
 

 

Sample Input
2 3 2 1 2 3 1 1 1 3 1 2 2 3 2 1 2 3 1 1 2 3 1 2 2 2 1 2 1
 

 

Sample Output
Case #1: -1 Case #2: 4
 

 

Author
Thost & Kennethsnow
 

Noi2008 志愿者招募 同样 就是相邻的节点  不是连续的天数了 而是创建了一个图ios

用dfs走一遍  建图就行了ide

公式不用推  看懂 那个题想一下就行了ui

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e5 + 10, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int n, m, s, t;
int head[maxn], d[maxn], vis[maxn], nex[maxn], f[maxn], p[maxn], cnt, head1[maxn], nex1[maxn];
int xu[maxn], flow, value, ans;

struct edge
{
    int u, v, c;
}Edge[maxn << 1];

void addedge(int u, int v, int c)
{
    Edge[ans].u = u;
    Edge[ans].v = v;
    Edge[ans].c = c;
    nex1[ans] = head1[u];
    head1[u] = ans++;
};



struct node
{
    int u, v, w, c;
}Node[maxn << 1];

void add_(int u, int v, int w, int c)
{
    Node[cnt].u = u;
    Node[cnt].v = v;
    Node[cnt].w = w;
    Node[cnt].c = c;
    nex[cnt] = head[u];
    head[u] = cnt++;
}

void add(int u, int v, int w, int c)
{
    add_(u, v, w, c);
    add_(v, u, -w, 0);
}

int spfa()
{
    for(int i = 0; i < maxn; i ++) d[i] = INF;
    deque<int> Q;
    mem(vis, 0);
    mem(p, -1);
    Q.push_front(s);
    d[s] = 0;
    p[s] = 0, f[s] = INF;
    while(!Q.empty())
    {
        int u = Q.front(); Q.pop_front();
        vis[u] = 0;
        for(int i = head[u];i != -1; i = nex[i])
        {
            int v = Node[i].v;
            if(Node[i].c)
            {
                if(d[v] > d[u] + Node[i].w)
                {
                    d[v] = d[u] + Node[i].w;
                    p[v] = i;
                    f[v] = min(f[u], Node[i].c);
                    if(!vis[v])
                    {
                      //  cout << v << endl;
                        if(Q.empty()) Q.push_front(v);
                        else
                        {
                            if(d[v] < d[Q.front()]) Q.push_front(v);
                            else Q.push_back(v);
                        }
                        vis[v] = 1;
                    }
                }
            }
        }
    }
    if(p[t] == -1) return 0;
    flow += f[t], value += f[t] * d[t];
   // cout << value << endl;
    for(int i = t; i != s; i = Node[p[i]].u)
    {
        Node[p[i]].c -= f[t];
        Node[p[i] ^ 1].c += f[t];
    }
    return 1;
}

void max_flow()
{
    flow = value = 0;
    while(spfa());
}
int sum_flow;

void init()
{
    mem(head, -1);
    mem(head1, -1);
    Edge[0].c = 0;
    cnt = sum_flow = 0;
    ans = 1;
}

void dfs(int u, int pre_sum)
{
    int sum = 0;
    for(int i = head1[u]; i != -1; i = nex1[i])
    {
        int v = Edge[i].v;
        add(u, v, 0, INF);
        dfs(v, Edge[i].c);
        sum += Edge[i].c;   //要减去当前子节点的全部父节点的公式
    }
    int tmp = pre_sum - sum;
    if(tmp > 0) add(s, u, 0, tmp), sum_flow += tmp;
    else add(u, t, 0, -tmp);

}


int id[maxn];

int main()
{
    int T, kase = 0;
    int u, v, w, c;
    rd(T);
    while(T--)
    {
        init();
        rd(n);
        s = 0, t = n + 2;
        rap(i, 1, n - 1)
        {
            rd(u), rd(v), rd(w);
            addedge(v, u, w);   //反向建图  想一下是下一个公式减去上一个公式  即子结点减去父结点
        }
        addedge(t, 1, 0);
        rd(m);
        rap(i, 1, m)
        {
            rd(u), rd(v), rd(c), rd(w);
            add(u, v, w, c);
        }
        dfs(1, 0);
        max_flow();
        printf("Case #%d: ", ++kase);
        if(sum_flow == flow)
            cout << value << endl;
        else
            cout << -1 << endl;


    }


    return 0;
}

 

River Problem

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 721    Accepted Submission(s): 282


this

Problem Description
The River of Bitland is now heavily polluted. To solve this problem, the King of Bitland decides to use some kinds of chemicals to make the river clean again.

The structure of the river contains n nodes and exactly n-1 edges between those nodes. It's just the same as all the rivers in this world: The edges are all unidirectional to represent water flows. There are source points, from which the water flows, and there is exactly one sink node, at which all parts of the river meet together and run into the sea. The water always flows from sources to sink, so from any nodes we can find a directed path that leads to the sink node. Note that the sink node is always labeled 1.

As you can see, some parts of the river are polluted, and we set a weight Wi for each edge to show how heavily polluted this edge is. We have m kinds of chemicals to clean the river. The i-th chemical can decrease the weight for all edges in the path from Ui to Vi by exactly 1. Moreover, we can use this kind of chemical for Li times, the cost for each time is Ci. Note that you can still use the chemical even if the weight of edges are 0, but the weight of that edge will not decrease this time.

When the weight of all edges are 0, the river is cleaned, please help us to clean the river with the least cost.
 

 

Input
The first line of the input is an integer T representing the number of test cases. The following T blocks each represents a test case.

The first line of each block contains a number n (2<=n<=150) representing the number of nodes. The following n-1 lines each contains 3 numbers U, V, and W, means there is a directed edge from U to V, and the pollution weight of this edge is W. (1<=U,V<=n, 0<=W<=20)

Then follows an number m (1<=m<=2000), representing the number of chemical kinds. The following m lines each contains 4 numbers Ui, Vi, Li and Ci (1<=Ui,Vi<=n, 1<=Li<=20, 1<=Ci<=1000), describing a kind of chemical, as described above. It is guaranteed that from Ui we can always find a directed path to Vi.
 

 

Output
First output "Case #k: ", where k is the case numbers, then follows a number indicating the least cost you are required to calculate, if the answer does not exist, output "-1" instead.
 

 

Sample Input
2 3 2 1 2 3 1 1 1 3 1 2 2 3 2 1 2 3 1 1 2 3 1 2 2 2 1 2 1
 

 

Sample Output
Case #1: -1 Case #2: 4
 

 

Author
Thost & Kennethsnow
 

 

本身选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要可以为了当时纯粹的梦想和感动坚持努力下去,无论其它人怎么样,咱们也可以保持本身的本色走下去。