A + B Problem

这道题没啥意思,就是拉来温暖一下榜单的
Code:

#include<stdio.h>
int main()
{
    int a,b;
    while(~scanf("%d %d",&a,&b))
    printf("%d\n",a+b);
    return 0;
}

A + B Problem II

Description:
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

Problem solving:
这道题是经典的大数相加问题,c++选手得用模拟来写。
java选手就很舒服了。但是初衷是让大家用c++写。

java就不再解释了,直接大数搞就行了。

c++我这里用了string来模拟,仔细看代码吧,还是很有意思的。
Code:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin >> n;
    int m = 0;
    int l = 0;
    for (int i = 1; i <= n; i++)
    {
        string s1, s2, s(10000, '0');
        cin >> s1 >> s2;
        m++;
        cout << (l++ ? "\n" : "");
        reverse(s1.begin(), s1.end());
        reverse(s2.begin(), s2.end());
        for (int j = 0; j < s1.length(); j++)
            s[j] = s1[j];
        int temp = 0;
        for (int k = 0; k < s2.length(); k++)
        {
            temp += s[k] - 48 + s2[k] - 48;
            s[k]  = temp % 10 + '0';
            temp /= 10;
        }
        s[s2.length()] = s[s2.length()] - 48 + temp + 48;
        reverse(s.begin(), s.end());
        reverse(s1.begin(), s1.end());
        reverse(s2.begin(), s2.end());
        cout << "Case" << m << ":" << endl;
        cout << s1 << "+" << s2 << "=" << s.substr(s.find_first_not_of('0')) << endl;
    }
    return 0;
}

Code:

import java.math.*;
import java.util.*;
import java.*;
public class Main
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        int t = cin.nextInt();
        int num = 1;
        int x=0;
        while(t>0)
        {
            if(x!=0)    System.out.println("");
            else x=1;
            BigInteger a = cin.nextBigInteger();
            BigInteger b = cin.nextBigInteger();
            System.out.println("Case "+num+":");
            System.out.println(a+" + "+b+" = "+a.add(b));
            num++;            
            t--;
        }
    }
}

Drainage Ditches

Description:
Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
Input
The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
Output
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
Sample Input
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
Sample Output
50
Problem solving:
可以去看看这个学长写的这道题的题解(课件内容大多来自这里
https://boctorio.com/2019/08/14/%E7%BD%91%E7%BB%9C%E6%B5%81/
这道题真的是板子题,源点为1,汇点为n,直接套dinic就能过。
Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f;
const ll maxn = 1e6+10;
ll n,m,s,t,u,v,w,cnt;
struct edge{
    ll to,dis,nxt;
}G[maxn];
ll head[maxn],d[maxn],cur[maxn];
void add(ll from,ll to,ll dis)
{
    G[cnt].to=to;
    G[cnt].dis=dis;
    G[cnt].nxt=head[from];
    head[from]=cnt++;
}
ll dfs(ll u,ll flow)
{
    if(u==t)    return flow;
    ll x=0;
    for(ll i=cur[u];i!=-1;i=G[i].nxt)
    {
        cur[u]=G[i].nxt;
        ll v=G[i].to;
        if(d[v]==d[u]+1&&G[i].dis>0)
        {
            ll res=dfs(v,min(flow,G[i].dis));
            flow-=res;
            x+=res;
            G[i].dis-=res;
            G[i^1].dis+=res;
            if(flow==0) break;
        }
    }
    if(!x)  d[u]=-1;
    return x;
}
void bfs()
{
    memset(d,-1,sizeof(d));
    queue<ll> que;
    que.push(s);
    d[s]=0;
    while(!que.empty())
    {
        ll u=que.front();que.pop();
        for(ll i=head[u];i!=-1;i=G[i].nxt)
        {
            ll v=G[i].to;
            if(d[v]==-1&&G[i].dis>0)
            {
                d[v]=d[u]+1;
                que.push(v);
            }
        }
    }
}

void dinic()
{
    ll max_flow=0;
    while(1)
    {
        bfs();
        if(d[t]==-1)    break;
        for(ll i=1;i<=n;i++)    cur[i]=head[i];
        max_flow+=dfs(s,INF);
    }
    cout<<max_flow<<endl;
}

int main()
{
    while(cin>>m>>n)
    {
        s=1,t=n;
        memset(head,-1,sizeof(head));
        cnt=0;
        for(ll i=0;i<m;i++)
        {
            cin>>u>>v>>w;
            add(u,v,w);
            add(v,u,0);
        }
        dinic();
    }
    return 0;
}

Island Transport

Description:
In the vast waters far far away, there are many islands. People are living on the islands, and all the transport among the islands relies on the ships.
  You have a transportation company there. Some routes are opened for passengers. Each route is a straight line connecting two different islands, and it is bidirectional. Within an hour, a route can transport a certain number of passengers in one direction. For safety, no two routes are cross or overlap and no routes will pass an island except the departing island and the arriving island. Each island can be treated as a point on the XY plane coordinate system. X coordinate increase from west to east, and Y coordinate increase from south to north.
  The transport capacity is important to you. Suppose many passengers depart from the westernmost island and would like to arrive at the easternmost island, the maximum number of passengers arrive at the latter within every hour is the transport capacity. Please calculate it.
Input
  The first line contains one integer T (1<=T<=20), the number of test cases.
  Then T test cases follow. The first line of each test case contains two integers N and M (2<=N,M<=100000), the number of islands and the number of routes. Islands are number from 1 to N.
  Then N lines follow. Each line contain two integers, the X and Y coordinate of an island. The K-th line in the N lines describes the island K. The absolute values of all the coordinates are no more than 100000.
  Then M lines follow. Each line contains three integers I1, I2 (1<=I1,I2<=N) and C (1<=C<=10000) . It means there is a route connecting island I1 and island I2, and it can transport C passengers in one direction within an hour.
  It is guaranteed that the routes obey the rules described above. There is only one island is westernmost and only one island is easternmost. No two islands would have the same coordinates. Each island can go to any other island by the routes.
Output
  For each test case, output an integer in one line, the transport capacity.
Sample Input
2
5 7
3 3
3 0
3 1
0 0
4 5
1 3 3
2 3 4
2 4 3
1 5 6
4 5 3
1 4 4
3 4 2
6 7
-1 -1
0 1
0 2
1 0
1 1
2 3
1 2 1
2 3 6
4 5 5
5 6 3
1 4 6
2 5 5
3 6 4
Sample Output
9
6
Problem solving:
这道题看着就很恶心,实际上确实也很恶心。他给了你10s;10秒!!!
其实这道题跟C差不多的,只不过它的源点和汇点需要自己找出来,找的时候也很简单。我们只看x轴的坐标就行了。在最左边的看成是源点,在最右边的看成是汇点。
然后套板子
当你信心满满觉得要过的时候,你会发现,哦豁,TLE了。
如果你跟我一样用的是cin和cout,那么你一定会想到是cin和cout的锅。然后关闭同步流,信心满满的觉得要过得时候,你发现他还是会tle。
然后如果你跟我还一样,你会把cin和cout换成scanf和printf。当你信心满满的觉得这次一定会过的时候,你就又tle了。然后你会想到会不会是板子的锅。
是不是板子的锅我也不知道,应该是我的姿势不够优雅,所以用不了板子写这个题。

这道题讲道理就是一道简单的板子题,但是卡时间卡的比较严重,所以你需要各种优化。这道题是我拉的不好了,这种题没啥意思。
Code:

#include<bits/stdc++.h.>
using namespace std;
const int N = 100010;
const int INF = 0x3f3f3f3f;
typedef long long ll;
int n,m,start,over;
int deep[N],maxflow;
struct edge{
    int to,w,pre;
}a[N<<2];
int cnt=-1,head[N];
void add(int from,int to,int w)
{
    a[++cnt].to=to;
    a[cnt].w=w;
    a[cnt].pre=head[from];
    head[from]=cnt;
}
bool bfs()//这里我们不用队列直接用数组代替
{
    memset(deep,-1,sizeof(deep));
    int q[N<<1];
    int fro,bac;
    fro=bac=0;
    q[bac++]=start,deep[start]=0;
    while(fro<bac)
    {
        int first=q[fro];
        if(first==over) return 1;
        for(int i=head[first];~i;i=a[i].pre)
        {
            if(deep[a[i].to]==-1&&a[i].w>0)
            {
                deep[a[i].to]=deep[first]+1;
                q[bac++]=a[i].to;
            }
        }
        fro++;
    }
    return 0;
}

int dfs(int s,int cap)
{
    if(s==over) return cap;
    int flow=0,f;
    for(int i=head[s];~i;i=a[i].pre)
    {
        int to=a[i].to;
        if(deep[to]==deep[s]+1&&a[i].w)
        {
            f=dfs(to,min(cap-flow,a[i].w));
            a[i].w-=f;
            a[i^1].w+=f;
            flow+=f;
            if(flow==cap)   break;
        }
    }
    if(flow==0) deep[s]=-2;
    return flow;
}
void dinic()
{
    int tem=0;
    while(bfs())
        while((tem=dfs(start,INF))>0)
            maxflow+=tem;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        maxflow=0,cnt=-1;
        memset(head,-1,sizeof(head));
        scanf("%d %d",&n,&m);
        start = over = 1;
        int x,y,z,x_min=INF,x_max=-INF;
        for(int i=1;i<=n;i++)
        {
            scanf("%d %d",&x,&y);
            if(x<x_min) start=i,x_min=x;
            if(x>x_max) over=i,x_max=x;
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d %d %d",&x,&y,&z);
            add(x,y,z);
            add(y,x,z);
        }
        dinic();
        cout<<maxflow<<endl;
    }
    return 0;
}

Power Network

Description:
A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) >= 0 of power, may produce an amount 0 <= p(u) <= p max(u) of power, may consume an amount 0 <= c(u) <= min(s(u),c max(u)) of power, and may deliver an amount d(u)=s(u)+p(u)-c(u) of power. The following restrictions apply: c(u)=0 for any power station, p(u)=0 for any consumer, and p(u)=c(u)=0 for any dispatcher. There is at most one power transport line (u,v) from a node u to a node v in the net; it transports an amount 0 <= l(u,v) <= l max(u,v) of power delivered by u to v. Let Con=Σ uc(u) be the power consumed in the net. The problem is to compute the maximum value of Con.


An example is in figure 1. The label x/y of power station u shows that p(u)=x and p max(u)=y. The label x/y of consumer u shows that c(u)=x and c max(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and l max(u,v)=y. The power consumed is Con=6. Notice that there are other possible states of the network but the value of Con cannot exceed 6.
Input
There are several data sets in the input. Each data set encodes a power network. It starts with four integers: 0 <= n <= 100 (nodes), 0 <= np <= n (power stations), 0 <= nc <= n (consumers), and 0 <= m <= n^2 (power transport lines). Follow m data triplets (u,v)z, where u and v are node identifiers (starting from 0) and 0 <= z <= 1000 is the value of l max(u,v). Follow np doublets (u)z, where u is the identifier of a power station and 0 <= z <= 10000 is the value of p max(u). The data set ends with nc doublets (u)z, where u is the identifier of a consumer and 0 <= z <= 10000 is the value of c max(u). All input numbers are integers. Except the (u,v)z triplets and the (u)z doublets, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end of file and are correct.
Output
For each data set from the input, the program prints on the standard output the maximum amount of power that can be consumed in the corresponding network. Each result has an integral value and is printed from the beginning of a separate line.
Sample Input
2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20
7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 (2,4)7
(3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5
(0)5 (1)2 (3)2 (4)1 (5)4
Sample Output
15
6
Hint
The sample input contains two data sets. The first data set encodes a network with 2 nodes, power station 0 with pmax(0)=15 and consumer 1 with cmax(1)=20, and 2 power transport lines with lmax(0,1)=20 and lmax(1,0)=10. The maximum value of Con is 15. The second data set encodes the network from figure 1.
Problem solving:
这个题意也很恶心
(原题描述的过于复杂,原题中的s[i]根本不用管)总共有n个节点,其中有发电站np个、用户nc个和调度器n-np-nc个三种节点以及M条电线(用于传输电流的),每个发电站有一个最大发电量,每个用户有个最大接受电量,现在有m条有向边,边有一个最大的电流量,表示最多可以流出这么多电,现在从发电站发电到用户,问最多可以发多少电(被用户接受).

分析:
建立一个超级源点s,s到任意发电站i有(s,i,,p[i]) (表示该发电站最多能发p[i]电)
建立一个超级汇点t,任意用户j到t有边(j,t,c[j]) (表示该用户最多能消费c[j]电)
然后对于题目中描述的M条电线(u,v)L, 就有边(u,v,L).
最终我们求本图的最大流即可.(到汇点t的最大流就是所有用户能消费的最大电量)

然后这道题也是一个简单的板子题啦,主要是要自己确定一个源点和一个汇点。就是输入有点恶心。
把输入搞定也就是套板子了。在这里我没有用弧优化。

Code:

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
const int INF = 0x3f3f3f3f;
const int maxn = 2e5;
using namespace std;
struct edge{
    int to,dis,nxt;
}G[maxn];
int head[maxn],d[maxn];
int n,np,nc,m,cnt,s,t;
void add(int from,int to,int dis)
{
    G[cnt].to=to;
    G[cnt].dis=dis;
    G[cnt].nxt=head[from];
    head[from]=cnt++;
}
int bfs()
{
    memset(d,-1,sizeof(d));
    d[s]=0;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        if(u==t)    return 1;
        q.pop();
        for(int i=head[u];i!=-1;i=G[i].nxt)
        {
            int v=G[i].to;
            int dis=G[i].dis;
            if(dis&&d[v]==-1)
            {
                d[v]=d[u]+1;
                q.push(v);
            }
        }
    }
    return 0;
}
int dfs(int u,int flow)
{
    if(u==t) return flow;
    int x=0;
    for(int i=head[u];i!=-1;i=G[i].nxt)
    {
        int v=G[i].to;
        int dis=G[i].dis;
        if(dis&&d[v]==d[u]+1)
        {
            int f=dfs(v,min(dis,flow-x));
            G[i].dis-=f;
            G[i^1].dis+=f;
            x+=f;
            if(x==flow) return x;
        }
    }
    return x;
}
void dinic()
{
    int ans=0;
    while(bfs())
    {
        ans+=dfs(s,INF);
    }
    cout<<ans<<endl;
}
int main()
{
    while(~scanf("%d%d%d%d",&n,&np,&nc,&m))
    {
        cnt=0;
        s=n+1;
        t=s+1;
        memset(head,-1,sizeof(head));
        for(int i=0;i<m;i++)
        {
            int x,y,w;
            scanf(" (%d,%d)%d",&x,&y,&w);
            add(x,y,w);
            add(y,x,0);
        }
        for(int i=0;i<np;i++)
        {
            int v,w;
            scanf(" (%d)%d",&v,&w);
            add(s,v,w);
            add(v,s,0);
        }
        for(int i=0;i<nc;i++)
        {
            int u,w;
            scanf(" (%d)%d",&u,&w);
            add(u,t,w);
            add(t,u,0);
        }
        dinic();
    }
}

Path

最后这道题是这位学长拉的,题解去看他的就完事了
https://blog.csdn.net/cloudy_happy/article/details/97107699