250731总结
怎么又是图论!qwq
才写6个题,我太菜了qwq
A - Great Graphs
写了13分钟,一遍过
先按权值排序,全部整成一条链,然后对每两个点连一条权值从大的到小的负的边就可以愉快的AC叻:)
#include <bits/stdc++.h>
#define LL long long
using namespace std;
LL t, n, a[100010];
int main () {
for (cin >> t; t--;) {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a + 1, a + n + 1);
LL ans = a[n];
for (int i = 1; i < n; i++) {
ans += (a[i] - a[i + 1]) * (n - i) * i;
}
cout << ans << "\n";
}
return 0;
}
B - Fight Against Traffic
写了33分钟,WA一发qwq
先跑2边bfs求出每个点
WA一发是因为j从i + 1开始枚举但是没判断
#include <bits/stdc++.h>
#define LL long long
using namespace std;
int n, m, s, t, d[2][1010], a[1010][1010], ans;
vector <int> v[1010];
void bfs (int ss, int tmp) {
queue <int> q;
q.push(ss);
d[tmp][ss] = 0;
while (q.size()) {
int x = q.front();
q.pop();
for (int i : v[x]) {
if (d[tmp][i] > d[tmp][x] + 1) {
d[tmp][i] = d[tmp][x] + 1;
q.push(i);
}
}
}
}
int main () {
cin >> n >> m >> s >> t;
for (int i = 1, x, y; i <= m; i++) {
cin >> x >> y;
v[x].emplace_back(y);
v[y].emplace_back(x);
a[x][y] = a[y][x] = 1;
}
fill(&d[0][0], &d[1][n + 1], 1e9);
bfs(s, 0);
bfs(t, 1);
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (a[i][j]) continue;
if (d[0][i] + d[1][j] + 1 >= d[0][t] && d[1][i] + d[0][j] + 1 >= d[0][t]) {
ans++;
}
}
}
cout << ans;
return 0;
}
C - Tax
写了30分钟,一遍过
直接bfs出每个点的最短路,然后暴力dfs枚举所有合法的路径(只要一个点的最短路是与它相邻的点的最短路+1这条边就是合法的)。
#include <bits/stdc++.h>
#define LL long long
using namespace std;
int n, m, w[2010], d[60], ans[60], f[2010];
struct aa {
int x, c;
};
vector <aa> v[60];
void bfs () {
fill(d + 1, d + n + 1, 1e9);
d[1] = 0;
queue <int> q;
q.push(1);
while (q.size()) {
int x = q.front();
q.pop();
for (aa i : v[x]) {
if (d[i.x] > d[x] + 1) {
d[i.x] = d[x] + 1;
q.push(i.x);
}
}
}
}
void dfs (int x, int sum) {
ans[x] = min(ans[x], sum);
for (aa i : v[x]) {
if (d[i.x] == d[x] + 1) {
dfs(i.x, sum + (++f[i.c]) * w[i.c]);
f[i.c]--;
}
}
}
int main () {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> w[i];
}
for (int i = 1, x, y, c; i <= m; i++) {
cin >> x >> y >> c;
v[x].emplace_back((aa){y, c});
v[y].emplace_back((aa){x, c});
}
bfs();
fill(ans + 1, ans + n + 1, 1e9);
dfs(1, 0);
for (int i = 2; i <= n; i++) {
cout << ans[i] << "\n";
}
return 0;
}
D - Exploration
写了1h,WA两发,T一发qwq
不难发现,最多只会走30步,所以可以写一个dp,求出每个点的每个步数最大的乘积是多少,询问的时候枚举步数就行
WA是因为转移搞错了,T是因为long long和ios
开了long long见祖宗
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int inf = 2e9;
int n, m, q, f[200010][35];
struct aa {
int x, v;
};
vector <aa> v[200010];
int main () {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m >> q;
for (int i = 1, x, y, d; i <= m; i++) {
cin >> x >> y >> d;
v[x].emplace_back((aa){y, d});
}
fill(&f[0][0], &f[n + 1][0], 1);
for (int i = 1; i <= 30; i++) {
for (int j = 1; j <= n; j++) {
for (aa k : v[j]) {
f[j][i] = max(f[j][i], (int)min(1ll * f[k.x][i - 1] * k.v, 1ll * inf));
}
}
}
for (int p, x; q--;) {
cin >> p >> x;
for (int i = 0; i <= 30; i++) {
if (f[p][i] > x) {
cout << i << "\n";
break;
}
}
}
return 0;
}
E - 渡劫
写了53分钟,WA两发
欸这不是跟小鳄鱼hhc一起打ACM的题吗:)但是当时除了hhc没人知道怎么做qwq但是我听到了建一个超级源点这句话:)
建一个2层的分层图,每条边从上面往下面连一个边权为0的,用来处理法宝。然后建一个超级源,连到上面那层,边权为a,然后跑一遍dj,相当于倒过来求随机到每个点的最短路,就可以愉快地AC叻:)
WA数组开小导致的qwq
#include <bits/stdc++.h>
#define LL long long
using namespace std;
LL n, m, a[100010], dis[200010], vis[200010], cnt, s, ans;
struct edge {
LL to, v, h, nxt;
} e[4000010];
void add (int x, int y, LL z) {
e[++cnt].v = z;
e[cnt].to = y;
e[cnt].nxt = e[x].h;
e[x].h = cnt;
}
void dj () {
priority_queue <pair <LL, int>> q;
q.push({0, s});
fill(dis + 1, dis + s + 1, 1e18);
dis[s] = 0;
while (q.size()) {
int x = q.top().second;
q.pop();
if (vis[x]) continue;
vis[x] = 1;
for (int i = e[x].h; i; i = e[i].nxt) {
int v = e[i].to;
if (dis[v] > dis[x] + e[i].v) {
dis[v] = dis[x] + e[i].v;
q.push({-dis[v], v});
}
}
}
}
int main () {
cin >> n >> m;
s = n * 2 + 1;
for (LL i = 1, u, v, w; i <= m; i++) {
cin >> u >> v >> w;
add(u, v, w);
add(v, u, w);
add(u + n, v + n, w);
add(v + n, u + n, w);
add(u + n, v, 0);
add(v + n, u, 0);
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
add(s, i + n, a[i]);
add(i + n, i, 0);
}
dj();
for (int i = 1; i <= n; i++) {
ans = max(ans, dis[i]);
}
cout << ans;
return 0;
}
I - 工作调度
写了45分钟,T一发
一眼拓扑:)第二眼就不知道咋拓扑了
然后想到先把每一类拓扑一下搞出类的顺序,第二次按工作拓扑的时候用优先队列就好了:)
T是因为ios
#include <bits/stdc++.h>
#define LL long long
using namespace std;
int t, n, m, a[100010], f[100010], c, ans[100010], ind[100010];
struct aa {
int x, y;
} e[200010];
vector <int> v[100010];
bool tp1 () {
queue <int> q;
for (int i = 1; i <= n; i++) {
if (!ind[i]) {
q.push(i);
}
}
while (q.size()) {
int x = q.front();
q.pop();
f[x] = ++c;
for (int i : v[x]) {
ind[i]--;
if (!ind[i]) {
q.push(i);
}
}
}
return c >= n;
}
bool tp2 () {
priority_queue <pair <int, int>> q;
for (int i = 1; i <= n; i++) {
if (!ind[i]) {
q.push({-f[a[i]], i});
}
}
while (q.size()) {
int x = q.top().second;
q.pop();
ans[++c] = x;
for (int i : v[x]) {
ind[i]--;
if (!ind[i]) {
q.push({-f[a[i]], i});
}
}
}
return c >= n;
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
for (cin >> t; t--;) {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
ind[i] = 0;
}
for (int i = 1; i <= n; i++) {
v[i].clear();
}
c = 0;
for (int i = 1; i <= m; i++) {
cin >> e[i].x >> e[i].y;
if (a[e[i].x] == a[e[i].y]) continue;
v[a[e[i].x]].emplace_back(a[e[i].y]);
ind[a[e[i].y]]++;
}
if (!tp1()) {
cout << "-1\n";
continue;
}
for (int i = 1; i <= n; i++) {
v[i].clear();
}
for (int i = 1; i <= m; i++) {
v[e[i].x].emplace_back(e[i].y);
ind[e[i].y]++;
}
c = 0;
if (!tp2()) {
cout << "-1\n";
continue;
}
for (int i = 1; i <= n; i++) {
cout << ans[i] << ' ';
}
cout << "\n";
}
return 0;
}