UESTC 482 Charitable Exchange(四川省赛B题)

Description

Have you ever heard a star charity show called Charitable Exchange? In this show, a famous star starts with a small item which values $1$ yuan. 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 $R_i$ yuan, with a time cost of $T_i$ minutes.

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

Input

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

For each case, two integers $N$, $M$ ($1 \leq N \leq 10^5$, $1 \leq M \leq 109$) in the first line indicates the number of available exchanges and the expected value of final item. Then $N$ lines follow, each line describes an exchange with $3$ integers $V_i$, $R_i$, $T_i$ ($1 \leq R_i \leq V_i \leq 10^9$, $1 \leq T_i \leq 109$).

Output

For every test case, you should output Case #k: first, where $k$ indicates the case number and counts from $1$. Then output the minimum time. Output $-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

Hint




一开始深搜超时,没想到优先队列+bfs可以做,,感觉复杂度差不多。。具体细节只要将能换到的钱从小到大排序,每次弹出最小的时间线性找过去就好了。。

#include
#include 
using namespace std;
#include 
#include 
typedef long long ll;
#include 
const int maxn=100005;
int len;
int n;
ll zmoney;
struct Edge
{
    ll u,v,time;
} edge[maxn];
bool cmp(Edge a,Edge b)
{
    return a.ub.time;
    }
};
ll bfs()
{
    priority_queueq;
    int x=1;
    int i;
    node st,ed;
    st.money=1;
    st.time=0;
    q.push(st);
    while(!q.empty())
    {
        ed=q.top();
        q.pop();
        if(ed.money>=zmoney)
            return ed.time;
        for(i=x; i<=len; i++)
        {
            if(ed.money=edge[i].u&&edge[i].v>ed.money)
            {
                st.money=edge[i].v;
                st.time=ed.time+edge[i].time;
                q.push(st);
            }
        }
        x=i;
    }
    return -1;
}
int main()
{
    int t;
    scanf("%d",&t);
    int icase=0;
    while(t--)
    {
        cin>>n>>zmoney;
        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,cmp);
        printf("Case #%d: ",++icase);
        ll ans=bfs();
        cout<

你可能感兴趣的:(优先队列,搜索)