Acwing 903 昂贵的聘礼
题目描述
思路
首先从题目中抽象出来模型,即求以编号\(1\)的点为终点,求最短距离。终点确定了,但我们并没有确定起点,可以使用一个虚拟源点作为起点,该虚拟源点到其他点的距离为够买该编号商品原本的价钱。这样问题就变成了以虚拟源点\(S\)为起点,以编号\(1\)为终点的最短路。由于等级限制只有\(100\),所以我们可以枚举等级区间,对于每个等级区间的最短路取\(min\)即可。由于\(1\)号商品是一定要买的,所以我们枚举的区间为\([level_{1}-m,level_{1}]\)。
AC代码
#include "iostream"
#include "cstring"
#include "string"
#include "vector"
#include "cmath"
#include "algorithm"
#include "map"
#include "set"
#include "queue"
#include "stack"
#include "cassert"
#include "unordered_map"
#include "sstream"
#include "cstdio"
using namespace std;
#define fi first
#define se second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) a.begin(),a.end()
#define rep(x,l,u) for(ll x=l;x=u;x--)
#define sz(x) x.size()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define seteps(N) setprecision(N)
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
#define lson (ind<<1)
#define rson (ind<<1|1)
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 lll;
typedef pair PII;
typedef pair PCC;
typedef pair PDD;
typedef pair PLL;
typedef pair PIII;
struct Scanner {
bool hasNext = 1;
bool hasRead = 1;
int nextInt() {
hasRead = 0;
int res = 0;
char flag = 1, ch = getchar();
while(ch != EOF && !isdigit(ch)) {
hasRead = 1;
flag = (ch == '-') ? -flag : flag;
ch = getchar();
}
while(ch != EOF && isdigit(ch)) {
hasRead = 1;
res = res * 10 + (ch - '0');
ch = getchar();
}
if(ch == EOF)
hasNext = 0;
return res * flag;
}
ll nextLL() {
hasRead = 0;
ll res = 0;
char flag = 1, ch = getchar();
while(ch != EOF && !isdigit(ch)) {
hasRead = 1;
flag = (ch == '-') ? -flag : flag;
ch = getchar();
}
while(ch != EOF && isdigit(ch)) {
hasRead = 1;
res = res * 10 + (ch - '0');
ch = getchar();
}
if(ch == EOF)
hasNext = 0;
return res * flag;
}
char nextChar() {
hasRead = 0;
char ch = getchar();
while(ch != EOF && isspace(ch)) {
hasRead = 1;
ch = getchar();
}
if(ch == EOF)
hasNext = 0;
return ch;
}
int nextString(char *str) {
hasRead = 0;
int len = 0;
char ch = getchar();
while(ch != EOF && isspace(ch)) {
hasRead = 1;
ch = getchar();
}
while(ch != EOF && !isspace(ch)) {
hasRead = 1;
str[++len] = ch;
ch = getchar();
}
str[len + 1] = 0;
if(ch == EOF)
hasNext = 0;
return len;
}
} sc;
ll rd() {
ll x = sc.nextLL();
return x;
}
void rd(int &x) {
x = sc.nextInt();
}
void rd(ll &x) {
x = sc.nextLL();
}
void rd(char &x) {
x = sc.nextChar();
}
void rd(char* x) {
sc.nextString(x);
}
template
void rd(pair &x) {
rd(x.first);
rd(x.second);
}
template
void rd(T *x, int n) {
for(int i = 1; i <= n; ++i)
rd(x[i]);
}
template
void rd(vector &x,int n){
for(int i = 1; i <= n; ++i)
rd(x[i]);
}
void printInt(int x) {
if(x < 0) {
putchar('-');
x = -x;
}
if(x >= 10)
printInt(x / 10);
putchar('0' + x % 10);
}
void printLL(ll x) {
if(x < 0) {
putchar('-');
x = -x;
}
if(x >= 10)
printLL(x / 10);
putchar('0' + x % 10);
}
void pr(int x, char ch = '\n') {
printInt(x);
putchar(ch);
}
void pr(ll x, char ch = '\n') {
printLL(x);
putchar(ch);
}
//#define LOCAL
template
void pr(pair x, char ch = '\n') {
#ifdef LOCAL
putchar('<');
pr(x.first, ' ');
pr(x.second, '>');
putchar(ch);
return;
#endif //LOCAL
pr(x.first, ' ');
pr(x.second, ch);
}
template
void pr(T *x, int n) {
for(int i = 1; i <= n; ++i)
pr(x[i], " \n"[i == n]);
}
template
void pr(vector &x) {
int n = x.size();
for(int i = 1; i <= n - 1; ++i)
pr(x[i], " \n"[i == n - 1]);
}
const int N=105;
const int M=205;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const int mod1=998244353;
const lll oone=1;
const double eps=1e-6;
const double pi=acos(-1);
int n,m;
int g[N][N],level[N],d[N];
bool st[N];
int dijkstra(int down,int up){
mst(d,INF);
mst(st,false);
d[0]=0;
rep(i,0,n){
int t=-1;
rep(j,0,n+1) if(!st[j] && (t==-1 || d[t]>d[j])) t=j;
rep(j,1,n+1){
if(level[j]>=down && level[j]<=up){
d[j]=min(d[j],d[t]+g[t][j]);
}
}
st[t]=true;
}
return d[1];
}
void solve(){
rd(m);
rd(n);
mst(g,INF);
rep(i,1,n+1) g[i][i]=0;
rep(i,1,n+1){
int p,l,x;
rd(p);rd(l);rd(x);
g[0][i]=min(p,g[0][i]);
level[i]=l;
rep(j,0,x){
int idx,price;
rd(idx);rd(price);
g[idx][i]=min(g[idx][i],price);
}
}
int ans=INF;
rep(i,level[1]-m,level[1]+1){
ans=min(ans,dijkstra(i,i+m));
}
pr(ans);
}
int main(){
//IOS;
//freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
//int t;rd(t);
//rep(i,0,t){
//printf("Case #%d: ", i+1);
solve();
//}
return 0;
}