Have you ever played the Mario game? Of course you did, who did not?! Anyway, a new version of the Mario game has been released, it is some kind of kart racing game. And you decided to write a program to find the best strategy for you to complete each level. Each level track can be modeled as an infinite straight line, with some stations at some specific points on this line. Each station has an integer, representing its position on the track. Your task is to go from the first station (the one with smallest position) to the last one (the one with largest position) in the minimum number of moves. You can move between any two stations directly (you can go to a non-adjacent station, or you can go back to a station with a lower position if you want!) if you have enough boost coins for that move. In each level, you have some boost coins that you can use. Each boost coin has a cost and power value. You can select any subset of the coins for each move, but each coin may be used only once per move. Coins are permanent, and you can use the coins again for other moves in the same level. To make a move, you must choose a subset of the boost coins, such that the sum of their costs must not exceed L, and the sum of their power values must be exactly equal to the absolute difference between the positions of the two stations you are moving between. If there is no such subset, then you cannot make that move directly. Now you are given some configurations for some levels, and you are required to find the minimum number of moves to finish each one, or say it is impossible to finish that level. Input Your program will be tested on one or more test cases. The first line of the input will be a single integer T, the number of test cases (1 ≤ T ≤ 100). Followed by the test cases, the first line of each test case contains 3 integers separated by a single space N M L (2 ≤ N ≤ 100), (1 ≤ M ≤ 100) and (1 ≤ L ≤ 1,000) representing the number of stations, the number of boost coins and the maximum sum of coins’ costs in each move, respectively. Followed by a line which contains N unique positive integers separated by a single space (not necessary sorted, and each integer will be at most 1,000), representing the positions of the stations. Followed by M lines, each line contains 2 integers separated by a single space C V (1 ≤ C, V ≤ 100) which represent the cost and the power value of a coin, respectively. Output For each test case, print a single line which contains a single integer, this integer should be -1 if there is no way to go from the left most station to the right most station, or the minimum number of moves to do it if it is possible. Examples mario.
2
3 2 4
3 1 6
3 2
3 3
3 1 4
1 3 6
3 2
2
-1
一条路上有n个车站,你有m个金币,金币有各自的费用和价值,可以通过金币在车站间移动 每次移动要选择一些金币,总成本不能超过L,其价值和必须刚好等于abs(i-j) 问从1号站移动到n号站最少移动多少次 。
来源2013ICPC
#include<bits/stdc++.h>
#include<bitset>
#include<unordered_map>
#define pb push_back
#define bp __builtin_popcount
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=1e3+100;
const int MOD=1e9+7;
const double PI=3.1415926535;
int lowbit(int x){return x&-x;}
ll gcd(ll x, ll y){ return y == 0 ? x : gcd(y, x%y); }
ll lcm(ll x, ll y){ return x / gcd(x, y)*y; }
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }
int p[maxn], c[maxn], v[maxn];
int dp[maxn], dis[maxn], vis[maxn], mp[maxn][maxn], t, n, m, l;
void build()
{
for(int i=1;i<=n;++i)
{
for(int j=1;j<=n;++j)
{
if(p[i]==p[j])
mp[i][j]=0;
else mp[i][j]=1e9+7;
}
}
for(int i=1;i<=n;++i)
{
for(int j=i+1;j<=n;++j)
{
if(dp[p[j]-p[i]]<=l)
mp[i][j]=mp[j][i]=1;
}
}
}
void dijkstra(int u)
{
memset(vis, 0, sizeof vis);
for (int i = 1; i <= n;i++)
dis[i] = mp[u][i];
vis[u] = 1;
dis[u] = 0;
for (int i = 1; i < n;i++)
{
int mm = 1e9+7, id = u;
for (int j = 1; j <= n;j++)
{
if(dis[j]<mm&&!vis[j])
{
mm = dis[j];
id = j;
}
}
vis[id] = 1;
for (int j = 1; j <= n;j++)
{
if(!vis[j]&&dis[j]>dis[id]+mp[id][j])
{
dis[j] = dis[id] + mp[id][j];
}
}
}
if(dis[n]==1e9+7) cout<<-1<<endl;
else cout<<dis[n]<<endl;
}
int main()
{
cin >> t;
while(t--)
{
cin >> n >> m >> l;
for (int i = 1; i <= n;i++)
{
cin >> p[i];
}
for (int i = 1; i <= m;i++)
cin>>c[i]>>v[i];
sort(p, p + 1 + n);
memset(dp, 1e9+7, sizeof dp);
dp[0]=0;
for (int i = 1; i <= m;i++)
{
for (int j = p[n] - p[1]; j >= v[i];j--)
{
dp[j] = min(dp[j], dp[j - v[i]] + c[i]);
}
}
build();
dijkstra(1);
}
//system("pause");
return 0;
}
/*2
3 2 4
3 1 6
3 2
3 3
3 1 4
1 3 6
3 2
*/