hdu 3339

题解以后再补上,是一道最小生成树加01背包的题

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int M = 2000000;
const int INF = 1e+9;
const int N = 110;

struct edge{
    int to,cost;
    edge(int t,int c){
        to = t,cost = c;
    }
};

typedef pair<int,int> pii;
std::vector<edge> es[N];
int T,n,m;
int d[N],v[N],dp[M];

void dijkstra(int s){
    priority_queue<pii,vector<pii>,greater<pii> > que;
    fill(d,d+N,INF);
    d[s] = 0;
    que.push(pii(0,s));

    while(!que.empty()){
        pii p = que.top();que.pop();
        int v = p.second;
        if (d[v] < p.first) continue;
        for (int i = 0;i < es[v].size();++i){
            edge e = es[v][i];
            if (d[e.to] > d[v] + e.cost){
                d[e.to] = d[v] + e.cost;
                que.push(pii(d[e.to],e.to));
            }
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> T;
    while(T--){
        for (int i = 0;i < N;++i){
            es[i].clear();
        }
        int sum = 0;
        cin >> n >> m;
        for (int i = 0;i < m;++i){
            int s,t,c;
            cin >> s >> t >> c;
            es[s].push_back(edge(t,c));
            es[t].push_back(edge(s,c));
        }
        for (int i = 1;i <= n;++i){
            cin >> v[i];
            sum += v[i];
        }
        dijkstra(0);
        fill(dp,dp+M,INF);
        dp[0] = 0;
        for (int i = 1;i <= n;++i){
            for (int j = sum;j >= v[i];--j){
                dp[j] = min(dp[j],dp[j-v[i]]+d[i]);
            }
        }
        int x = sum/2+1,ans = INF;
        for (int i = x;i <= sum;++i){
            ans = ans > dp[i] ? dp[i] : ans;
        }
        if (ans == INF) cout << "impossible" << endl;
        else cout << ans << endl;
    }



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值