UESTC - 482

Have you ever heard a star charity show called Charitable Exchange? In this show, a famous star starts with a small item which values 11yuan. Then, through the efforts of repeatedly exchanges which continuously increase the value of item in hand, he (she) finally brings back a valuable item and donates it to the needy.

In each exchange, one can exchange for an item of Vi yuan if he (she) has an item values more than or equal to RiRi yuan, with a time cost of TiTi minutes.

Now, you task is help the star to exchange for an item which values more than or equal to MM yuan with the minimum time.

Input

The first line of the input is TT (no more than 2020), which stands for the number of test cases you need to solve.

For each case, two integers NNMM (1N1051≤N≤1051M1091≤M≤109) in the first line indicates the number of available exchanges and the expected value of final item. Then NN lines follow, each line describes an exchange with 33 integers ViViRiRiTiTi (1RiVi1091≤Ri≤Vi≤1091Ti1091≤Ti≤109).

Output

For every test case, you should output Case #k: first, where kkindicates the case number and counts from 11. Then output the minimum time. Output 1−1 if no solution can be found.

Sample Input
3
3 10
5 1 3
8 2 5
10 9 2
4 5
2 1 1
3 2 1
4 3 1
8 4 1
5 9
5 1 1
10 4 10
8 1 10
11 6 1
7 3 8
Sample Output
Case #1: -1
Case #2: 4

Case #3: 10

题目大意:T组数据,你本来有一块钱,可以用R块钱去换值V块钱的东西,但是得花费T个时间,最后问达到m的钱需要花费多长时间(当然是最少)。

刚开始一想,这提莫就是个最短路啊。连接头尾加时间就好了。

然后想了一下spfa。 可能我每次想到最短路的时候就想到这个东西吧。

后面看了题解才想起优先队列。差不多其实跟dij的思想如出一辙吧。

不过存数据的时候先排个序。

#include <bits/stdc++.h>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;


#define pi acos(-1)
#define endl '\n'
#define me(x) memset(x,0,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0);
const int INF=0x3f3f3f3f;
//const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,-1,-1,1,1};
const int dy[]={0,1,0,-1,1,-1,1,-1};
const int maxx=1e5+100;
const double EPS=1e-7;
const int MOD=1000000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
/*lch[root] = build(L1,p-1,L2+1,L2+cnt);
    rch[root] = build(p+1,R1,L2+cnt+1,R2);中前*/
/*lch[root] = build(L1,p-1,L2,L2+cnt-1);
    rch[root] = build(p+1,R1,L2+cnt,R2-1);中后*/
long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}

typedef long long ll;
const int maxn=1e5+100;
int len;
int n;
ll m;
struct Edge
{
    ll u,v,time;
    bool friend operator<(Edge a,Edge b)
    {
        return a.u<b.u;
    }
} edge[maxn];
struct node
{
    ll money;
    ll time;
    bool friend operator<(node a,node b)
    {
        return a.time>b.time;
    }
};
ll bfs()
{
    priority_queue<node>q;
    int i=1;
    node ed;
    q.push(node{1,0});
    while(!q.empty())
    {
        ed=q.top();
        q.pop();
        if(ed.money>=m)
            return ed.time;
        for(;i<=len;i++)
        {
            if(ed.money<edge[i].u)
                break;
            if(ed.money>=edge[i].u&&edge[i].v>ed.money)
            q.push(node{edge[i].v,ed.time+edge[i].time});
        }
    }
    return -1;
}
int main()
{
    int t;
    cin>>t;
    for(int cas=1;cas<=t;cas++)
    {
        cin>>n>>m;
        len=1;
        for(int i=1;i<=n;i++)
        {
            ll a,b,c;
            cin>>a>>b>>c;
            if(a==b) continue;
            edge[len].u=b;
            edge[len].v=a;
            edge[len++].time=c;
        }
        sort(edge+1,edge+len+1);
        printf("Case #%d: %lld\n",cas,bfs());
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值