求代码。
【题目描述】
C 国拥有 n 座城市,城市由 m 条双向道路所连通,每条道路需要通行的时间均为1。
现在 A 君想在满足能在不超过l1时间内从s1到t1且能在不超过l2时间内从s2到t2的情况下摧毁 C 国尽量多的道路。
请你告诉 A 君,最多能摧毁多少条道路。摧毁道路后剩下的城市不一定需要都连通。
【输入格式】
第一行两个整数n,m,表示城市数与道路数,城市从1 到 n编号。
接下来 m 行,每行两个整数u,v 表示一条道路。保证一条道路不会出现多次。
最后两行每行三个整数,第一行为s1,t1,l1,第二行为s2,t2,l2
。
【输出格式】
输出一行一个整数表示答案。若无法满足条件则输出−1。
样例输入 1
5 4
1 2
2 3
3 4
4 5
1 3 2
2 4 2
样例输出
1
1
样例输入
2
9 9
1 2
2 3
2 4
4 5
5 7
5 6
3 8
8 9
9 6
1 7 4
3 6 4
样例输出
2
3
【数据范围】
对于20% 的数据,n,m≤15;
另有20% 的数据,m=n−1;
另有20% 的数据,s1=s2,t1=t2;
另有20% 的数据,s1=s2;
对于100%的数据,1≤n,m≤3000,1≤u,v≤n且u≠v,1≤s[i],t[i]≤n,0≤l[i]≤n。
原代码 80 分
#include <bits/stdc++.h>
using namespace std;
struct edge {
int v, w, next;
};
struct node {
int dis, id;
bool operator<(const node& a) const {
return dis > a.dis;
}
node(int d, int x) : dis(d), id(x) {}
};
const int MAXN = 5005;
const int INF = INT_MAX;
edge e[10005];
int head[MAXN], vis[MAXN], cnt;
long long h[MAXN];
vector<vector<int>> di(MAXN, vector<int>(MAXN, INF));
vector<int> dis(MAXN, INF);
void add(int uu, int vv, int ww) {
e[++cnt] = {vv, ww, head[uu]};
head[uu] = cnt;
}
void dijkstra(int s) {
priority_queue<node> q;
fill(dis.begin(), dis.end(), INF);
fill(vis, vis + MAXN, 0);
dis[s] = 0;
q.push(node(0, s));
while (!q.empty()) {
int u = q.top().id;
q.pop();
if (vis[u]) continue;
vis[u] = 1;
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].v;
if (dis[v] > dis[u] + e[i].w) {
dis[v] = dis[u] + e[i].w;
if (!vis[v]) q.push(node(dis[v], v));
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int uu, vv;
cin >> uu >> vv;
add(uu, vv, 1);
add(vv, uu, 1);
}
for (int i = 1; i <= n; ++i) {
dijkstra(i);
di[i] = dis;
}
int ans = INF;
int s1, t1, l1, s2, t2, l2;
cin >> s1 >> t1 >> l1 >> s2 >> t2 >> l2;
if (di[s1][t1] > l1) {
cout << -1;
return 0;
}
if (di[s2][t2] > l2) {
cout << -1;
return 0;
}
for (int j = 1; j <= n; ++j) {
for (int k = 1; k <= n; ++k) {
int l = di[j][k];
if (l == INF || di[s1][j] == INF || di[k][t1] == INF || di[s2][j] == INF || di[k][t2] == INF) continue;
if (di[s1][j] + l + di[k][t1] <= l1 &&
di[s2][j] + l + di[k][t2] <= l2) {
ans = min(ans, di[s1][j] + l + di[k][t1] + di[s2][j] + di[k][t2]);
}
}
}
ans = min(ans, di[s1][t1] + di[s2][t2]);
if (ans == INF) cout << -1;
else cout << m - ans;
return 0;
}
80 Wrong Answer
# 状态分数 耗时 内存占用
#1 Accepted5 51ms 96.3 MiB
#2 Wrong Answer0 51ms 96.4 MiB
#3 Accepted5 51ms 96.5 MiB
#4 Accepted5 51ms 96.3 MiB
#5 Accepted5 52ms 96.3 MiB
#6 Wrong Answer0 80ms 96.4 MiB
#7 Accepted5 214ms 96.4 MiB
#8 Accepted5 348ms 96.6 MiB
#9 Accepted5 62ms 96.4 MiB
#10 Accepted5 108ms 96.4 MiB
#11 Accepted5 220ms 96.5 MiB
#12 Accepted5 239ms 96.6 MiB
#13 Accepted5 307ms 96.5 MiB
#14 Accepted5 51ms 96.4 MiB
#15 Accepted5 102ms 96.3 MiB
#16 Accepted5 230ms 96.4 MiB
#17 Wrong Answer0 237ms 96.6 MiB
#18 Accepted5 239ms 96.5 MiB
#19 Accepted5 246ms 96.6 MiB
#20 Wrong Answer0 228ms 96.4 MiB