ccpc network game hdu6705 path (queue simulation greedy

http://acm.hdu.edu.cn/showproblem.php?pid=6705

This is the second small number of questions before the game 8 had the problem, so they come up, but the feeling is not said than done. . (Fear not sign difficulty

Meaning of the questions: to a map, to a couple of choices, so you find the first k short circuit, all paths do not limit the number of uses.

Ideas: the shortest is certainly the shortest piece, the second short, there are two possibilities, it may be a short length of the second piece, then there may be just continue to take the shortest (two will not, but this first expanding), to the third short really is the 2 possible (e.g. 1,2,9,1 + 2 <9), but most obviously by shorting k k segments makes up (contradiction: if k + 1 Article , that the paragraph k + 1 will throw a little more, it is certainly not the best), so a direct cycle max (k) times, i took i-th cycle segment makes up the road into the update on the line. You can start priority queue, and the total length of the path end fitted struct.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e4+10;
struct node{
    ll to,len;
    node(){}
    node(ll a,ll b):to(a),len(b){}
};
struct cmp{
    bool operator ()(const node &x,const node &y) const{
        return x.len>y.len;
    }
};
bool cmp1(node a,node b){
    return a.len<b.len;
}
priority_queue<node,vector<node>,cmp > q0;
priority_queue<int> q1;
vector<node> ve[N];
ll a[N],b[N];
void init(int n){
    while(!q1.empty()) q1.pop();
    while(!q0.empty()) q0.pop();
    for(int i=1;i<=n;i++) ve[i].clear();
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t;
    cin>>t;
    while(t--){
        ll n,m,k;
        cin>>n>>m>>k;
        init(n);
        for(ll i=1;i<=m;i++){
            ll u,v,w;
            cin>>u>>v>>w;
            q0.push({v,w});
            q1.push(w);
            ve[u].push_back(node(v,w));
        }
        for(ll i=1;i<=n;i++){
            sort(ve[i].begin(),ve[i].end(),cmp1);
        }
        ll mx = 0;
        for(ll i=0;i<k;i++){
            cin>>a[i];
            mx = max(mx,a[i]);
        }
        for(ll i=1;i<=mx;i++){
            b[i] = q0.top().len;
            ll x = q0.top().to;
            q0.pop();
            for(int j=0;j<ve[x].size();j++){
                ll y=ve[x][j].to,len=b[i]+ve[x][j].len;
                if(q1.size()==mx){
                    if(len>q1.top()) break;
                    else{
                        q1.pop();
                        q1.push(len);
                        q0.push({y,len});
                    }
                }
                else {
                    q1.push(len);
                    q0.push({y,len});
                }
            }
        }
        for(ll i=0;i<k;i++) cout<<b[a[i]]<<endl;
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wzgg/p/11410161.html
Recommended