题意:
2015新年第一A ^_^ 新的一年里Fight on!!祝Mission Success!!!
。。
思路:
建图:task作节点,并且增加一个虚拟节点0,表示一个任务都没有完成的初始状态。i直接费用是从0到i的边,间接费用是从其他点到i的边。。
枚举:枚举所有[i, i+m]的区间就好,min(i) = max (0, lv[1]-m)
//#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cassert>
#include <algorithm>
#include <cmath>
#include <set>
#include <climits>
#include <map>
#include <iomanip>
using namespace std;
#define SPEED_UP iostream::sync_with_stdio(false);
#define FIXED_FLOAT cout.setf(ios::fixed, ios::floatfield);
#define rep(i, s, t) for(int (i)=(s);(i)<=(t);++(i))
#define urep(i, s, t) for(int (i)=(s);(i)>=(t);--(i))
#define in_bound(l, r, i) (l)<=(i)&&(i)<(r)
#define pb push_back
typedef long long LL;
const int inf = INT_MAX/2;
const int Maxn = 100;
int c[Maxn+5][Maxn+5], lv[Maxn+5], d[Maxn+5], n, m;
void add_edge(int from, int to, int cost) {
c[from][to] = cost;
//printf("edge: from %d to %d cost %d\n", from, to, cost);
}
int go(int l) {
//printf("range: %d-%d:\n", l, l+m);
typedef pair<int, int> P;
priority_queue<P, vector<P>, greater<P> > q;
fill(d, d+n+1, inf);
d[0] = 0;
q.push(make_pair(0, 0));
while (!q.empty()) {
P fr = q.top();q.pop();
if (fr.first != d[fr.second]) continue;
rep(i, 1, n)
if (lv[i] >= l && lv[i]-l <= m && c[fr.second][i] != -1 && d[fr.second]+c[fr.second][i] < d[i]) {
//printf("update %d from %d to %d: %d->%d\n", i, d[i], d[fr.second]+c[fr.second][i], fr.second, i);
d[i] = d[fr.second]+c[fr.second][i];
q.push(make_pair(d[i], i));
}
}
return d[1];
}
int solve() {
int ans = inf, lo = max (0, lv[1]-m);
rep(i, lo, lv[1])
ans = min (ans, go(i));
return ans;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
#endif
//SPEED_UP
cin >> m >> n;
memset(c, -1, sizeof(c));
int x, y, z;
rep(i, 1, n) {
cin >> x >> y >> z;
add_edge(0, i, x);
lv[i] = y;
rep(j, 1, z) {
cin >> x >> y;
add_edge(x, i, y);
}
}
//rep(i, 1, n) {rep(j, 1, n) cout << c[i][j] << ' ';cout << endl;}
cout << solve() << endl;
return 0;
}