2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 1004 path


#include <bits/stdc++.h>
#define mem(ar,num) memset(ar,num,sizeof(ar))
#define me(ar) memset(ar,0,sizeof(ar))
#define lowbit(x) (x&(-x))
#define Pb push_back
#define  FI first
#define  SE second
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define IOS ios::sync_with_stdio(false)
#define DEBUG cout<<endl<<"DEBUG"<<endl;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int    prime = 999983;
const int    INF = 0x7FFFFFFF;
const LL     INFF = 0x7FFFFFFFFFFFFFFF;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-6;
const LL     mod = 1e9 + 7;
LL qpow(LL a, LL b) {LL s = 1; while (b > 0) {if (b & 1)s = s * a % mod; a = a * a % mod; b >>= 1;} return s;}
LL gcd(LL a, LL b) {return b ? gcd(b, a % b) : a;}
int dr[2][4] = {1, -1, 0, 0, 0, 0, -1, 1};
typedef pair<int, int> P;

const int maxn = 1e5 + 10;
vector<P> G[maxn];
int n, m, q;

struct Val {
    LL prev, nowv;
    int from, th, to;
};
bool operator <(const Val &a, const Val &b) {
    return a.nowv < b.nowv;
}
bool operator > (const Val &a, const Val &b) {
    return a.nowv > b.nowv;
}
struct Query {
    int id, k;
} que[maxn];
LL ans[maxn];
bool operator <(const Query &a, const Query &b) {
    return a.k < b.k;
}
int main(void)
{
    int T; cin >> T;
    while (T--) {
        // int n;scanf("%d",&n);
        scanf("%d%d%d", &n, &m, &q);
        for (int i = 1; i <= n; ++i) G[i].clear();
        for (int i = 1; i <= m; ++i) {
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            G[u].Pb(P(w, v));
        }
        for (int i = 1; i <= n; ++i) {
            sort(G[i].begin(), G[i].end());
        }
        priority_queue<Val, vector<Val>, greater<Val>> Q;
        for (int i = 1; i <= n; ++i) {
            if (!G[i].empty())
                Q.push(Val{0, G[i][0].first, i, 0, G[i][0].second});
        }
        for (int i = 1; i <= q; ++i) {
            scanf("%d", &que[i].k);
            que[i].id = i;
        }
        sort(que + 1, que + q + 1);
        int j = 1;
        int cnt = 0;
        while (j <= q && !Q.empty()) {
            Val tmp = Q.top(); Q.pop();
            // cout << tmp.nowv << endl;
            cnt++;
            while (j <= q && que[j].k == cnt)
                ans[que[j].id] = tmp.nowv, ++j;

            if (tmp.th + 1 < G[tmp.from].size()) {
                Q.push(Val{tmp.prev, tmp.prev + G[tmp.from][tmp.th + 1].first, tmp.from, tmp.th + 1, G[tmp.from][tmp.th + 1].second});
            }
            if (!G[tmp.to].empty()) {
                Q.push(Val{tmp.nowv, tmp.nowv + G[tmp.to][0].first,            tmp.to, 0, G[tmp.to][0].second});
            }
        }
        for (int i = 1; i <= q; ++i)
            printf("%lld\n", ans[i]);

    }


    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值