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. Eureka+Hystrix(断路器、熔断器)

    红圈是断路器的三种状态: 关闭:1.当consumer访问provider时,在网络超时访问内,访问成功: 2.有时互相调用会出现网络涌动,(比如北京访问广东的服务器要经过很多次路由才能达到并相应), ...

  2. 云计算管理平台之OpenStack计算服务nova

    一.nova简介 nova是openstack中的计算服务,其主要作用是帮助我们在计算节点上管理虚拟机的核心服务:这里的计算节点就是指用于提供运行虚拟机实例的主机,通常像这种计算节点有很多台,那么虚拟 ...

  3. DOM属性/节点属性

    DOM属性:DOM(Document Object Model,文档对象模型)一种独立于语言,用于操作xml,html的应用编程接口1:获取节点: document.getElementById(id ...

  4. DTU连接经常遇到的问题有哪些

    随着物联网的不断推进,工业.环保.能源.共享等领域对于DTU设备的应用也越来越广泛,在应用过程中,DTU经常遇到哪些问题以及解决办法,下面做如下分析. 第一,DTU如何与组态软件连接? 答:二者连接的 ...

  5. codeforces 1442 A. Extreme Subtraction(贪心,构造)

    传送门 样例(x): 8 15 16 17 19 27 36 29 33 结果(t1) 15 15 16 18 26 35 28 32 思路:我们可以把最左端和最右端当做两个水龙头,每个水龙头流量的上 ...

  6. CSS中的position属性笔记

    一般有5个属性,分别是:static,absolute,relative,fixed,inherit static 自然定位:这个是默认值,没有定位,再设置top,rignt,bottom,left会 ...

  7. 全排列算法--递归实现(Java)

    求一个n阶行列式,一个比较简单的方法就是使用全排列的方法,那么简述以下全排列算法的递归实现. 首先举一个简单的例子说明算法的原理,既然是递归,首先说明一下出口条件.以[1, 2]为例 首先展示一下主要 ...

  8. PS中抠图的四种方法介绍

    工具/原料 photoshop 软件(我用的是photoshop cc) 需要抠图的图片 开始的步骤 打开ps 打开图片,ctrl+O 魔棒抠图法 对于前景和后景有明显差别的图片用魔棒抠图法抠图比较容 ...

  9. C++ 数据结构 1:线性表

    1 数据结构 1.1 数据结构中基本概念 数据:程序的操作对象,用于描述客观事物. 数据的特点: 可以输入到计算机 可以被计算机程序处理 数据是一个抽象的概念,将其进行分类后得到程序设计语言中的类型. ...

  10. php 正则金额验证

    $money_reg = '/^[1-9]\d*|^[1-9]\d*.\d+[1-9]$/';if(!preg_match($money_reg, $money)){ $this->ajaxEr ...