8.1日总结


打到一半已经不想写了,来写博客吧,希望能进队


7.31地址
C题:
题意:给定一个长为n,宽位m的矩形,然后两个人依次往矩形里面画半径为n的圆,问先画的人会赢还是后画的人会赢

解法:类比NIM博弈,你先手一个动作,如果我能通过模仿你的动作让局势再回到最开始的状态,就能让你输。这道题也一样,只要这个圆不大到让后手所画的圆构不成对称图形,那么先手就输了。圆最大的标准就是它不会大的超过最短边的一半

代码有点走样,关键是上边懂了就好

#include
#include
#include
using namespace std;

int main() {
    int T; scanf("%d", &T);
    while (T--) {
        int a, b, r; 
        cin >> a >> b >> r;
        int tmp = 4*r*r;
        if (tmp<(a*a + b*b) && a*a>tmp && b*b > tmp)
            cout << "The first one is winner." << endl;
        else cout << "The second one is winner." << endl;
    }
    return 0;
}

8.1场地址
A题:
题意:给定一个数,求大于等于它的第一个(2^a*3^b*5^c*7^d)的数
解法:打表,之后二分即可

#include
#include
#include
using namespace std;
typedef long long ll;
const int maxn = 1e7 + 5;
ll sum[maxn];
multisets;
int tot = 0;

ll poww(ll a, ll b) {
    ll ans = 1, base = a;
    while (b != 0) {
        if (b & 1 != 0)ans *= base;
        base *= base; b >>= 1;
    }
    return ans;
}

//这里的30是试出来的
void generate() {
    for (int i = 0; i < 30; i++) {
        ll ti = poww(7, i);
        for (int j = 0; j < 30; j++) {
            ll tj = poww(5, j);
            for (int k = 0; k < 30; k++) {
                ll tk = poww(3, k);
                for (int l = 0; l < 30; l++) {
                    ll tl = poww(2, l);
                    ll now = ti*tj*tk*tl;
                    if (now > 7e9+1)break;
                    else s.insert(now);
                }
            }
        }
    }
    return;
}


int main() {
    int T; scanf("%d", &T);
    generate();
    while (T--) {
        int n; scanf("%d", &n);
        printf("%lld\n", *s.lower_bound(n));
    }
    return 0;
}

B题:
题意:给出一个数n,我们想求出Σ(1/(k*k)),k属于1~n

解法:打表,由于题目要求精度为小数点后5位,那么当k大于1e6的时候公式的值累加就已经不起作用了,这也是题目为什么不告诉你n的原因


#include
#include
#include
#include
using namespace std;
const int maxn = 1e6;
string s;
double sum[maxn + 7];

int main() {
    sum[0] = 0;
    for (int i = 1; i <= maxn; i++)
        sum[i] = sum[i - 1] + double(1.0 / (1.0*i *1.0* i));
    while (cin >> s) {
        if (s.size() > 6) {printf("%.5lf\n", sum[maxn]);continue;}
        int ans = 0;
        for (int i = 0; i < s.size(); i++) ans = ans * 10 + s[i] - '0';
        printf("%.5lf\n", double(sum[ans]));
    }
    return 0;
}

J题:
题意:有n个点,m条边,有一个人要从起点走到终点,他只会走最短路,你要往路上设置路障,让他至少碰到一次路障,每条路上的路障费用等于那条路的权值(路的长度为1),求设置路障的最小花费

解法:首先他只会走最短路,那么我就把所有最短路求出来,注意到所有路的长度都为1,那么只需要跑一遍最短路算法即可确定边的归属,之后我们想象敌人碰到一次路障之后就前进了,那么题目就转换成了至少割掉几条边起点到终点就不连通了,又因为边上有权值,那么这个问题就变成了一个最小割问题,和最大流等效,所以我们直接用Dicnic算法求解即可

#include
#include
#include
#include
#include
using namespace std;
const int maxn = 1e5 + 5;
const int maxx = 1000 + 5;
const int INF = 0x3f3f3f3f;
int n, m, s, t;

int g[maxx][maxx], val[maxx][maxx];
int dis[maxx], vis[maxx];
//dijkstra
void init(int n)
{
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            g[i][j] = INF;
        }
        dis[i] = INF;
    }
}

void dijkstra(int s, int n)
{
    dis[s] = 0;
    for (int i = 1; i < n; i++) {
        int mn = INF, x;
        for (int j = 1; j <= n; j++) {
            if (!vis[j] && dis[j] < mn)mn = dis[x = j];
        }
        vis[x] = 1;
        for (int j = 1; j <= n; j++) {
            dis[j] = min(dis[j], dis[x] + g[x][j]);
        }
    }
}


//最大流
struct Edge
{
    int from, to, cap, flow;
    Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f) {}
};
struct Dinic
{
    //  int n,m;
    int s, t;
    vectoredges;        //边数的两倍
    vector<int> G[maxn];      //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
    bool vis[maxn];           //BFS使用
    int d[maxn];              //从起点到i的距离
    int cur[maxn];            //当前弧下标
    void init()
    {
        for (int i = 0; i <= n + 1; i++)
            G[i].clear();
        edges.clear();
    }
    void AddEdge(int from, int to, int cap)
    {
        edges.push_back(Edge(from, to, cap, 0));
        edges.push_back(Edge(to, from, 0, 0));        //反向弧
        int mm = edges.size();
        G[from].push_back(mm - 2);
        G[to].push_back(mm - 1);
    }
    bool BFS()
    {
        memset(vis, 0, sizeof(vis));
        queue<int>q;
        q.push(s);
        d[s] = 0;
        vis[s] = 1;
        while (!q.empty())
        {
            int x = q.front(); q.pop();
            for (int i = 0; i < G[x].size(); i++)
            {
                Edge &e = edges[G[x][i]];
                if (!vis[e.to] && e.cap > e.flow)
                {
                    vis[e.to] = 1;
                    d[e.to] = d[x] + 1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int DFS(int x, int a)
    {
        if (x == t || a == 0)
            return a;
        int flow = 0, f;
        for (int &i = cur[x]; i < G[x].size(); i++)
        {
            Edge &e = edges[G[x][i]];
            if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0)
            {
                e.flow += f;
                edges[G[x][i] ^ 1].flow -= f;
                flow += f;
                a -= f;
                if (a == 0)
                    break;
            }
        }
        return flow;
    }

    int Maxflow(int s, int t)
    {
        this->s = s;
        this->t = t;
        int flow = 0;
        while (BFS())
        {
            memset(cur, 0, sizeof(cur));
            flow += DFS(s, INF);
        }
        return flow;
    }
}dc;

int main() {
    int T; scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &n, &m);
        init(n);
        for (int i = 0; i < m; i++) {
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            g[u][v] = g[v][u] = 1;
            val[u][v] = val[v][u] = w;
        }
        dijkstra(1, n);
        dc.init();
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (g[i][j] == 1 && dis[i] + 1 == dis[j]) {
                    dc.AddEdge(i, j, val[i][j]);
                }
        s = 1, t = n;
        printf("%d\n", dc.Maxflow(s, t));
    }
    return 0;
}

你可能感兴趣的:(2018暑假ACM集训)