E. Two Round Dances #圆排列

题目链接

题意

\(n\)(保证偶数)个人,要表演一个节目,这个节目包含两种圆形舞蹈,而每种圆形舞蹈恰好需要\(n/2\)个人,每个人只能跳一种圆形舞。

一个节目中两支舞蹈中的人编号组成一条圆环。故两个节目,对应两个圆环排列。两个不相同的节目,等价于,两个圆排列是不同的,现要你求出他们能做出多少种不同的节目。

分析

圆排列:从\(n\)个不同元素选取\(r\)个元素,不分首尾地围成一个圆圈的排列。

其排列方案数为:\(\frac{A^r_n}{r}\)。特别地,当\(n=r\)时,\(\frac{A^n_n}{n}\)可以进一步得到\((n-1)!\)

从\(n\)个人抽取\(\frac{n}{2}\)个人,有\(C^{\frac{n}{2}}_n\)分法,相应地另一组\(\frac{n}{2}\)也定下来。接下来考虑内部的排列,第一组的\(\frac{n}{2}\)个人进行圆排列有\((\frac{n}{2}-1)!\)个方案,第二组也有\((\frac{n}{2}-1)!\)个方案,由乘法原理得到最终答案:\(C^{\frac{n}{2}}_n \times (\frac{n}{2}-1)! \times(\frac{n}{2}-1)! \times \frac{1}{2}\). 最终答案之所以乘这个\(\frac{1}{2}\)是因为第一组的排列与第二组的排列可交换的。下面的代码是对上式进行了化简。

#include <string>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#include <vector>
#include <deque>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef long double ld;
const int MAXN = 75;
ll n;
int main(){
scanf("%lld", &n);
ll ans = 1;
for(int i = 1; i <= n; i++) ans *= (ll)i;
ans *= (ll)2;
ans /= (ll)(n * n);
printf("%lld\n", ans);
return 0;
}

G. Reducing Delivery Cost #最短路径 #暴力

题目链接

题意

\(n\)个点\(m\)条双向路,第\(i\)条路花费为\(w_i\)。现在有\(k\)个快递员,他们各自有不同的出发地\(a_i\)与目的地\(b_i\)。你现在可以从原\(m\)条双向路中选择至多一条的路,将该条路的花费置为\(0\)。现要你求出\(k\)个快递员的最小花费总和。其中\(2 \le n \le 1000, n - 1 \le m \le min(1000, \frac{n(n-1)}{2})\)

分析

由于题目需要我们询问多组的不同起点至终点的最短距离,同时考虑到数据的规模才\(1e3\),于是我们暴力地枚举每个顶点,计算从这个顶点出发到另外点的最短距离——通过\(dijkstra\)算法。

但是题目还没完,它还附加了条件,可以选择一条边权值至零。因为只能操作一条边,且边数量不大,直接枚举所有要删除的边,然后删去这个边后更新每个快递员的最短距离。

注意,在更新的过程中,一定要两个方向同时更新!

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <iostream>
using namespace std;
typedef long long ll;
typedef long long ld;
const int MAXN = 1e3 + 5;
int n, m, k, tot, H[MAXN], dis[MAXN][MAXN];
struct Edge {
int from, to, nextNbr, w;
}E[MAXN << 1];
void addEdge(int u, int v, int w) {
tot++;
E[tot].from = u;
E[tot].to = v;
E[tot].nextNbr = H[u];
E[tot].w = w;
H[u] = tot;
}
typedef struct qnode { //存放在优先队列中
int u, dis;
bool operator <(const struct qnode& oth) const {
return dis > oth.dis;
}
} qnode;
struct Route { //记录每个快递员的路径起点与终点
int st, ed;
}Route[MAXN];
void dijstra(int st) { //求出以st为起点,到达其他点的最短路径
for (int i = 1; i <= n; i++) dis[st][i] = 0x3f3f3f3f;
dis[st][st] = 0;
priority_queue<qnode> myque;
myque.push({ st, 0 });
while (!myque.empty()) {
int u = myque.top().u, curdis = myque.top().dis;
myque.pop();
if (curdis != dis[st][u]) continue;
for (int i = H[u]; i >= 0; i = E[i].nextNbr) {
int v = E[i].to, w = E[i].w;
if (u == v) continue;
if (dis[st][u] + w < dis[st][v]) {
dis[st][v] = dis[st][u] + w;
myque.push({ v, dis[st][v] });
}
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) H[i] = -1;
for (int i = 1, u, v, w; i <= m; i++) {
scanf("%d%d%d", &u, &v, &w);
addEdge(u, v, w);
addEdge(v, u, w);
}
int ans = 0x3f3f3f3f;
for (int i = 1; i <= n; i++) dijstra(i);
for (int i = 1; i <= k; i++) scanf("%d%d", &Route[i].st, &Route[i].ed);
for (int i = 1; i <= tot; i+=2) { //【先】枚举需要去除的边,因为只能去除一条边
int u = E[i].from, v = E[i].to;
int sum = 0;
for (int j = 1; j <= k; j++) { //【后】枚举去掉该边后,更新每个快递员的最短路径
int st = Route[j].st, ed = Route[j].ed;
sum += min(dis[st][ed],
min(dis[st][u] + dis[v][ed], dis[st][v] + dis[u][ed]));
} //注意!!!一定要从两个方向取最小值
ans = min(ans, sum);
}
printf("%d\n", ans);
return 0;
}

Codeforces Round #677 (Div. 3) E、G题解的更多相关文章

  1. Codeforces Round #677 (Div. 3) 题解

    Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...

  2. Codeforces Round #612 (Div. 2) 前四题题解

    这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...

  3. Codeforces Round #198 (Div. 2)A,B题解

    Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...

  4. Codeforces Round #672 (Div. 2) A - C1题解

    [Codeforces Round #672 (Div. 2) A - C1 ] 题目链接# A. Cubes Sorting 思路: " If Wheatley needs more th ...

  5. Codeforces Round #677 (Div. 3) G. Reducing Delivery Cost(dijkstra算法)

    题目链接:https://codeforces.com/contest/1433/problem/G 题解 跑 \(n\) 遍 \(dijkstra\) 得到任意两点间的距离,然后枚举哪一条边权为 \ ...

  6. Codeforces Round #677 (Div. 3)【ABCDE】

    比赛链接:https://codeforces.com/contest/1433 A. Boring Apartments 题解 模拟即可. 代码 #include <bits/stdc++.h ...

  7. Educational Codeforces Round 46 (Div 2) (A~G)

    目录 Codeforces 1000 A.Codehorses T-shirts B.Light It Up C.Covered Points Count(差分) D.Yet Another Prob ...

  8. Codeforces Round #614 (Div. 2) A-E简要题解

    链接:https://codeforces.com/contest/1293 A. ConneR and the A.R.C. Markland-N 题意:略 思路:上下枚举1000次扫一遍,比较一下 ...

  9. Codeforces Round #610 (Div. 2) A-E简要题解

    contest链接: https://codeforces.com/contest/1282 A. Temporarily unavailable 题意: 给一个区间L,R通有网络,有个点x,在x+r ...

随机推荐

  1. hello world .net core 微服务框架 Viper

    1.Viper是什么? Viper 是.NET平台下的Anno微服务框架的一个示例项目.入门简单.安全.稳定.高可用.全平台可监控.底层通讯可以随意切换thrift grpc. 自带服务发现.调用链追 ...

  2. 【USACO】New Years Party

    题意描述 New Years Party \(N(3\leq N\leq 200)\) 头奶牛举办新年聚会.每头奶牛会做几种不同的佳肴(以"碟"记数). 一共有 \(D(5\leq ...

  3. 【Kata Daily 191012】Find numbers which are divisible by given number

    题目: Complete the function which takes two arguments and returns all numbers which are divisible by t ...

  4. 获取List集合对象中某一列属性值

    例:获取disposeList集合中CorpusMarkPage对象中的responseId属性,生成新的List集合 List<String> responseIdList = disp ...

  5. ci框架根据配置自动生成controller控制器和model控制器(改版本)

    CI修改如下: if($modle_file=config_item('modle_file')) { if ($modle_file === TRUE) { $modle_file=config_i ...

  6. ubuntu常见问题有效解决办法

    1.关于weget "无法建立SSL连接"的解决方法 wget在使用HTTPS协议时,默认会去验证网站的证书,而这个证书验证经常会失败. 解决办法 原命令加上"--no- ...

  7. Redis下载

    Windows版下载地址 https://github.com/tporadowski/redis/releases Linux版下载地址 https://redis.io/download

  8. day002|python基础回顾2

    目录 00 上节课复习 01 基本数据类型 02 与用户交互 03 运算符 04 流程运算之if判断 05 流程判断之while循环 06 TEST 00 上节课复习 ""&quo ...

  9. ipmi常用的命令行命令

    前言 记录一些常用的命令行操作 命令 查询机器的电源状态 ipmitool -I lanplus -U admin -P admin -H 172.16.21.215 power status 硬重启 ...

  10. zabbix自动发现的python方式数据生成

    前言 zabbix里面有个功能是自动发现,比如文件系统和网卡的获取的时候,因为预先无法知道这个网卡的名称,所以就有了这个自动发现的功能,这里我是因为要用到存储池的自动发现,所以需要对数据进行生成 实现 ...