作者: KSkun

[APIO2013]机器人 题解

[APIO2013]机器人 题解

题目地址:洛谷:【P3638】[APIO2013]机器人 – 洛谷、BZOJ: 

[WC2008]游览计划 题解 & 斯坦纳树类题目解法

[WC2008]游览计划 题解 & 斯坦纳树类题目解法

题目地址:洛谷:【P4294】[WC2008]游览计划 – 洛谷、BZOJ:P 

[HDU4903]The only survival 题解

[HDU4903]The only survival 题解

题目地址:HDUOJ:Problem – 4903

题目描述

There is an old country and the king fell in love with a devil. The devil always ask the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.
Something bad actually happen. The devil makes this kingdom’s people infected by a disease called lolicon. Lolicon will take away people’s life in silence.
Although z*p is died, his friend, y*wan is not a lolicon. Y*wan is the only one in the country who is immune of lolicon, because he like the adult one so much.
As this country is going to hell, y*wan want to save this country from lolicon, so he starts his journey.
You heard about it and want to help y*wan, but y*wan questioned your IQ, and give you a question, so you should solve it to prove your IQ is high enough.
The problem is about counting. How many undirected graphs satisfied the following constraints?

  1. This graph is a complete graph of size n.
  2. Every edge has integer cost from 1 to L.
  3. The cost of the shortest path from 1 to n is k.

Can you solve it?
output the answer modulo 10^9+7
有一张n个点的无向完全图,第i个点的编号是i,每条边的边权在1到L之间的正整数,问存在多少个图使得1到n的最短路是k。

输入输出格式

输入格式:
The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains 3 integers n,k,L.
T<=5 n,k<=12,L<=10^9.

输出格式:
For each test case, output the answer in one line.

输入输出样例

输入样例#1:

2
3 3 3
4 4 4

输出样例#1:

8
668

题解

首先是方案数,我们就要找方案。考虑如果暴力地解决应该如何,我们可以首先把点1到每个点的最短路长度给枚举出来,然后构造这个图。但是仔细地分析一下,我们似乎不关心点的编号,只关心点的数量。那么这就好办了,我们只需要枚举1到该点最短路长度为定值的点有多少个就好了。
下面的叙述中,用dis代替某点最短路长度。
但是怎么统计方案数呢?考虑当前枚举到dis为x的点有i个的状况,枚举最短路长度1~x-1的方案数早已算出为res,且前面已经枚举过的点数和为tot,cnt数组表示dis为某值的点有多少个。
首先从剩下的点里面把i个点选出来,乘C_{n-tot-1}^i
这些点之间的边是无所谓的,所以每条边随便给个长度就行,乘L^{C_i^2}
dis更小的点向这些点连边,设当前枚举到了之前的dis为x'的情况,这些边要满足x' + w \geq x(j是dis更小的点,i是当前dis的点,w是这条边的边权),每条边的边权有L - (x - x') + 1种可以选择,但是如果全都选择了比x - x'大的边权,最短路长度就无法满足,因此有一部分方案不能满足,要减去,所以最后的方案数是 (L - (x - x') + 1)^{cnt_{x'}} - (L - (x - x'))^{cnt_{x'}}
把上面这一大堆乘进答案里就好啦。
到达枚举终点时tot还有可能比n小,即有点没有算过。这些点内部的边肯定是任意怎么样都行,但是要保证它们的dis比k大,这样的话,上面的“一部分方案不能满足”部分就不存在了。
注意这里计算的数据都挺大的,及时取模,小心溢出。

代码

// Code by KSkun, 2018/3
#include <cstdio>
#include <cstring>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}

inline LL readint() {
    register LL res = 0, neg = 1;
    char c = fgc();
    while(c < '0' || c > '9') {
        if(c == '-') neg = -1;
        c = fgc();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = fgc();
    }
    return res * neg;
}

const int MO = 1e9 + 7;

LL C[20][20];

inline void calc() {
    for(int i = 0; i <= 12; i++) {
        C[i][0] = 1;
        for(int j = 1; j <= i; j++) {
            C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MO;
        }
    }
}

inline LL fpow(LL x, LL k) {
    LL t = 1;
    while(k) {
        if(k & 1) t = (t * x) % MO;
        x = (x * x) % MO;
        k >>= 1;
    }
    return t;
}

int T, n, k, l, cnt[20];
LL ans;

inline LL cal(int x) {
    if(!cnt[x]) return 1;
    LL t1 = 1, t2 = 1;
    for(int i = 0; i < x; i++) {
        if(!cnt[i]) continue;
        if(x - i > l) return 0;
        t1 = t1 * fpow(l - (x - i) + 1, cnt[i]) % MO;
        t2 = t2 * fpow(l - (x - i), cnt[i]) % MO;
    }
    if(x == k + 1) return fpow(t1, cnt[x]);
    t1 -= t2; if(t1 < 0) t1 += MO;
    t1 = fpow(t1, cnt[x]);
    return t1;
}

inline void dfs(int x, LL res, int tot) {
    if(x == k) {
        for(int i = 1; i + tot <= n; i++) {
            LL nres = res * C[n - tot - 1][i - 1] % MO * fpow(l, C[i][2]) % MO * fpow(l, C[n - tot - i][2]) % MO;
            cnt[k] = i; cnt[k + 1] = n - tot - i;
            nres = nres * cal(k) % MO * cal(k + 1) % MO;
            ans = (ans + nres) % MO;
        }
        return;
    }
    for(int i = 0; i + tot < n; i++) {
        cnt[x] = i;
        LL nres = res * fpow(l, C[i][2]) % MO * C[n - tot - 1][i] % MO * cal(x) % MO;
        dfs(x + 1, nres, tot + i);
    }
}

int main() {
    calc();
    T = readint();
    while(T--) {
        n = readint(); k = readint(); l = readint();
        memset(cnt, 0, sizeof(cnt));
        cnt[0] = 1;
        ans = 0;
        dfs(1, 1, 1);
        printf("%lld\n", ans);
    }
    return 0;
}
[HDU6166]Senior Pan 题解

[HDU6166]Senior Pan 题解

题目地址:HDUOJ:Problem – 6166 题目描述 Senior P 

[HNOI2009]最小圈 题解

[HNOI2009]最小圈 题解

题目地址:洛谷:【P3199】[HNOI2009]最小圈 – 洛谷、BZOJ: 

[CF295B]Greg and Graph 题解

[CF295B]Greg and Graph 题解

题目地址:Codeforces:Problem – 295B – Codeforces、洛谷:【CF295B】Greg and Graph – 洛谷

题目描述

Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game:

  • The game consists of n steps.
  • On the i-th step Greg removes vertex number xi from the graph. As Greg removes a vertex, he also removes all the edges that go in and out of this vertex.
  • Before executing each step, Greg wants to know the sum of lengths of the shortest paths between all pairs of the remaining vertices. The shortest path can go through any remaining vertex. In other words, if we assume that d(i, v, u) is the shortest path between vertices v and u in the graph that formed before deleting vertex xi, then Greg wants to know the value of the following sum: \sum_{v, u, v \neq u} d(i, v, u).

Help Greg, print the value of the required sum before each step.
给一个完全图的邻接矩阵,按顺序删点,求每一次删点前剩下的点两两最短路长度的和。

输入输出格式

输入格式:
The first line contains integer n (1 ≤ n ≤ 500) — the number of vertices in the graph.
Next n lines contain n integers each — the graph adjacency matrix: the j-th number in the i-th line aij (1 ≤ aij ≤ 105, aii = 0) represents the weight of the edge that goes from vertex i to vertex j.
The next line contains n distinct integers: x1, x2, …, xn (1 ≤ xi ≤ n) — the vertices that Greg deletes.

输出格式:
Print n integers — the i-th number equals the required sum before the i-th step.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.

输入输出样例

输入样例#1:

1
0
1

输出样例#1:

0

输入样例#2:

2
0 5
4 0
1 2

输出样例#2:

9 0

输入样例#3:

4
0 3 1 1
6 0 400 1
2 4 0 1
1 1 1 0
4 1 2 3

输出样例#3:

17 23 404 0 

题解

n的范围很小,用O(n^2)的枚举求和是可行的,但是删点这个就不怎么好办了。
正难则反,如果把删点改成加点是否可以呢?
多源最短路让我们想到了Floyd,其实Floyd的实质是一个DP,由于压过维,所以有点看不出,实际上这个式子可以写成这样
dp[k][i][j]= \min (dp[k-1][i][j], dp[k-1][i][k] + dp[k-1][k][j])
如果在这个意义下,我们发现第一维维k的时候指的是只有1~k的图中的最短路。我们考虑把给出的排列反着来跑,计算出x[k]~x[n]的图的最短路,再把这些点的最短路每个算一遍加起来,加进答案序列。

代码

// Code by KSkun, 2018/3
#include <cstdio>

#include <algorithm>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}

inline LL readint() {
    register LL res = 0, neg = 1;
    char c = fgc();
    while(c < '0' || c > '9') {
        if(c == '-') neg = -1;
        c = fgc();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = fgc();
    }
    return res * neg;
}

const int MAXN = 505;

int n, dis[MAXN][MAXN], x[MAXN];
LL ans[MAXN];

int main() {
    n = readint();
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            dis[i][j] = readint();
        }
    }
    for(int i = 1; i <= n; i++) {
        x[i] = readint();
    }
    for(int k = n; k >= 1; k--) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                dis[i][j] = std::min(dis[i][j], dis[i][x[k]] + dis[x[k]][j]);
            }
        }
        for(int i = n; i >= k; i--) {
            for(int j = n; j >= k; j--) {
                ans[k] += dis[x[i]][x[j]];
            }
        }
    }
    for(int i = 1; i <= n; i++) {
        printf("%I64d ", ans[i]);
    }
    return 0;
}
[WF2012]Infiltration 题解

[WF2012]Infiltration 题解

题目地址:UVa:UVa Online Judge 题目描述 原题面参见UVa/vjudg 

[BZOJ2143]飞飞侠 题解

[BZOJ2143]飞飞侠 题解

题目地址:BZOJ:Problem 2143. — 飞飞侠 题目描述 飞飞国是 

[CF543B]Destroying Roads 题解

[CF543B]Destroying Roads 题解

题目地址:Codeforces:Problem – 666B – Codeforces、洛谷:【CF666B】World Tour – 洛谷

题目描述

In some country there are exactly n cities and m bidirectional roads connecting the cities. Cities are numbered with integers from 1 to n. If cities a and b are connected by a road, then in an hour you can go along this road either from city a to city b, or from city b to city a. The road network is such that from any city you can get to any other one by moving along the roads.
You want to destroy the largest possible number of roads in the country so that the remaining roads would allow you to get from city s1 to city t1 in at most l1 hours and get from city s2 to city t2 in at most l2 hours.
Determine what maximum number of roads you need to destroy in order to meet the condition of your plan. If it is impossible to reach the desired result, print -1.
求最多能删去的边数,且删去后满足dis[s1][t1]<=l1,dis[s2][t2]<=l2。

输入输出格式

输入格式:
The first line contains two integers n, m (1 ≤ n ≤ 3000, n - 1 \leq m \leq \min\{3000, \frac{n(n-1)}{2}\}) — the number of cities and roads in the country, respectively.
Next m lines contain the descriptions of the roads as pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi). It is guaranteed that the roads that are given in the description can transport you from any city to any other one. It is guaranteed that each pair of cities has at most one road between them.
The last two lines contains three integers each, s1, t1, l1 and s2, t2, l2, respectively (1 ≤ si, ti ≤ n, 0 ≤ li ≤ n).

输出格式:
Print a single number — the answer to the problem. If the it is impossible to meet the conditions, print -1.

输入输出样例

输入样例#1:

5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 2

输出样例#1:

0

输入样例#2:

5 4
1 2
2 3
3 4
4 5
1 3 2
2 4 2

输出样例#2:

1

输入样例#3:

5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 1

输出样例#3:

-1

题解

首先BFS预处理出所有点之间的最短路。如果dis[s1][t1]和dis[s2][t2]没删边就已经不满足了那删边是没有用的,这种情况就是无解的情况。
接着考虑在原本的最短路上有什么方法可以使最短路覆盖的边数减少。我们可以让两条路径中间有一段重合,兴许能减少边数。为什么是一段?因为如果有多段重合,把多段重合之间没重合的改成重合的显然更优。这样我们枚举重合的一段的端点,并且计算总长,更新答案。需要注意的是,s1/s2→u→v→t1/t2与s1→u→v→t1/s2→v→u→t2这两种情况都要考虑,因为是无向图可以双向通行嘛。
复杂度O(n^2)

代码

// Code by KSkun, 2018/3
#include <cstdio>
#include <cstring>

#include <vector>
#include <queue>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}

inline LL readint() {
    register LL res = 0, neg = 1;
    char c = fgc();
    while(c < '0' || c > '9') {
        if(c == '-') neg = -1;
        c = fgc();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = fgc();
    }
    return res * neg;
}

const int MAXN = 3005;

int n, m, ut, vt, s1, t1, l1, s2, t2, l2, dis[MAXN][MAXN], ans;
bool vis[MAXN];
std::vector<int> gra[MAXN];
std::queue<int> que;

inline void bfs(int st) {
    int *dist = dis[st];
    memset(vis, 0, sizeof(vis));
    que.push(st);
    vis[st] = true;
    dist[st] = 0;
    while(!que.empty()) {
        int u = que.front();
        que.pop();
        for(int v : gra[u]) {
            if(vis[v]) continue;
            vis[v] = true; 
            dist[v] = dist[u] + 1;
            que.push(v);
        }
    }
}

inline void updateans(int u, int v) {
    int c1 = dis[s1][u] + dis[u][v] + dis[v][t1], c2 = dis[s2][u] + dis[u][v] + dis[v][t2], c = c1 + c2 - dis[u][v];
    if(c1 <= l1 && c2 <= l2) ans = std::min(ans, c);
    c2 = dis[s2][v] + dis[u][v] + dis[u][t2], c = c1 + c2 - dis[u][v];
    if(c1 <= l1 && c2 <= l2) ans = std::min(ans, c);
}

int main() {
    memset(dis, 0x3f, sizeof(dis));
    n = readint(), m = readint();
    for(int i = 1; i <= m; i++) {
        ut = readint(), vt = readint();
        gra[ut].push_back(vt);
        gra[vt].push_back(ut);
    }
    s1 = readint(), t1 = readint(), l1 = readint();
    s2 = readint(), t2 = readint(), l2 = readint();
    for(int i = 1; i <= n; i++) {
        bfs(i);
    }
    if(dis[s1][t1] > l1 || dis[s2][t2] > l2) {
        printf("-1");
        return 0;
    }
    ans = dis[s1][t1] + dis[s2][t2];
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            updateans(i, j);
        }
    }
    printf("%d", m - ans);
    return 0;
}
[CF666B]World Tour 题解

[CF666B]World Tour 题解

题目地址:Codeforces:Problem – 666B –