题解

考虑一架飞机飞完自己之后还能飞到哪些航线,用floyd求两点最短路

这个图建出来是个DAG,求最小路径覆盖即可,二分图匹配

注意判断时是航班的起飞时刻+直飞时间+加油时间+最短路时间

代码

#include <bits/stdc++.h>
#define enter putchar('\n')
#define space putchar(' ')
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define MAXN 505
#define mo 99994711
#define pb push_back
#define eps 1e-8
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) out(x / 10);
putchar('0' + x % 10);
}
int N,M;
int g[MAXN][MAXN],P[MAXN],f[MAXN][MAXN];
int X[MAXN],Y[MAXN],D[MAXN];
bool vis[MAXN];
struct node {
int to,next;
}E[MAXN * MAXN];
int head[MAXN],sumE,matc[MAXN];
void add(int u,int v) {
E[++sumE].to = v;
E[sumE].next = head[u];
head[u] = sumE;
}
bool match(int u) {
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(!vis[v]) {
vis[v] = 1;
if(!matc[v] || match(matc[v])) {
matc[v] = u;
return true;
}
}
}
return false;
}
void Solve() {
read(N);read(M);
for(int i = 1 ; i <= N ; ++i) read(P[i]);
for(int i = 1 ; i <= N ; ++i) {
for(int j = 1 ; j <= N ; ++j) {
read(g[i][j]);
}
}
for(int i = 1 ; i <= N ; ++i) {
for(int j = 1 ; j <= N ; ++j) {
if(i == j) continue;
f[i][j] = g[i][j] + P[j];
}
}
for(int k = 1 ; k <= N ; ++k) {
for(int i = 1 ; i <= N ; ++i) {
for(int j = 1 ; j <= N ; ++j) {
f[i][j] = min(f[i][k] + f[k][j],f[i][j]);
}
}
}
for(int i = 1 ; i <= M ; ++i) {
read(X[i]);read(Y[i]);read(D[i]);
}
for(int i = 1 ; i <= M ; ++i) {
for(int j = 1 ; j <= M ; ++j) {
if(i == j) continue;
if(D[i] + g[X[i]][Y[i]] + P[Y[i]] + f[Y[i]][X[j]] <= D[j]) add(i,j);
}
}
int ans = M;
for(int i = 1 ; i <= M ; ++i) {
memset(vis,0,sizeof(vis));
if(match(i)) --ans;
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
05-11 21:44