| 比赛 |
期末考试1 |
评测结果 |
WAWWWWWWWW |
| 题目名称 |
Constructive |
最终得分 |
10 |
| 用户昵称 |
2_16鸡扒拌面 |
运行时间 |
0.149 s |
| 代码语言 |
C++ |
内存使用 |
4.13 MiB |
| 提交时间 |
2026-02-08 11:15:11 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=100;
const ll INF=1e18;
int N,x,y;
vector<int> a,b,c;
struct Node {
int p,q;
ll cost;
int x,y;
bool operator>(const Node& other) const {
if (cost!=other.cost) return cost>other.cost;
if (x!=other.x) return x>other.x;
return y>other.y;
}
};
int main() {
freopen("tioj_constructive.in", "r", stdin);
freopen("tioj_constructive.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cin>>N>>x>>y;
a.resize(N);
b.resize(N);
c.resize(N);
for (int i=0;i<N;i++) cin>>a[i]>>b[i]>>c[i];
vector<vector<ll>> dist(M,vector<ll>(M,INF));
vector<vector<int>> minX(M,vector<int>(M,1e9));
vector<vector<int>> minY(M,vector<int>(M,1e9));
priority_queue<Node, vector<Node>, greater<Node>> pq;
dist[0][0] = 0;
minX[0][0] = 0;
minY[0][0] = 0;
pq.push({0, 0, 0, 0, 0});
while (!pq.empty())
{
Node cur = pq.top(); pq.pop();
if (cur.cost > dist[cur.p][cur.q]) continue;
if (cur.x>minX[cur.p][cur.q]||cur.y>minY[cur.p][cur.q]) continue;
for (int i = 0; i < N; i++)
{
int np = (cur.p + a[i]) % M;
int nq = (cur.q + b[i]) % M;
ll ncost = cur.cost + c[i];
int nx = cur.x + a[i];
int ny = cur.y + b[i];
if (dist[np][nq]>ncost||(dist[np][nq]==ncost&&(nx<minX[np][nq]||(nx==minX[np][nq]&&ny<minY[np][nq]))))
{
dist[np][nq]=ncost;
minX[np][nq]=nx;
minY[np][nq]=ny;
pq.push({np, nq, ncost, nx, ny});
}
}
}
int tx=x%M,ty=y%M;
if (dist[tx][ty]==INF)
{
cout<<-1<<endl;
return 0;
}
int X0=minX[tx][ty],Y0 =minY[tx][ty];
if (X0 > x || Y0 > y)
{
cout << -1 << endl;
return 0;
}
int dx=x-X0,dy=y-Y0;
const int MAXD = 2000;
if (dx <= MAXD && dy <= MAXD) {
vector<vector<ll>> dp2(dx+1, vector<ll>(dy+1, INF));
dp2[0][0] = 0;
for (int i = 0; i <= dx; i++) {
for (int j = 0; j <= dy; j++) {
if (dp2[i][j] == INF) continue;
for (int k = 0; k < N; k++) {
int ni = i + a[k], nj = j + b[k];
if (ni <= dx && nj <= dy) {
dp2[ni][nj] = min(dp2[ni][nj], dp2[i][j] + c[k]);
}
}
}
}
if (dp2[dx][dy] == INF) cout << -1 << endl;
else cout << dist[tx][ty] + dp2[dx][dy] << endl;
}
else cout << -1 << endl;
return 0;
}