hdu3339 (dijkstra+ backpack)

Source: Internet
Author: User

Test instructions is easier to understand: each point has a POW value, represents electricity, requires the dispatch of some tanks from point No. 0, occupy these points, so that the total POW value of the occupied point is greater than the POW in the whole diagram. And the tank consumes the smallest total oil, the number of tanks is unlimited.

Train of thought, after obtaining the unit shortest circuit, take POW as backpack, path length for items, solve 01 backpack. Then sweep back from half of the total value of the POW to get the minimum amount of oil (item).

#include <iostream> #include <queue> #include <vector> #include <stdio.h> #define INF    100000000using namespace Std;int n,m;int dist[105];int dp[10000000],pow[105];struct node{int e,w; Node (int ee,int ww): E (EE), W (WW) {} node () {} friend bool operator< (node N1,node n2) {return N1. W>n2.    W    }};vector<vector<node> >v;void Dijikstra () {for (int i=1;i<=n;i++) Dist[i]=inf;    dist[0]=0;    Priority_queue<node> Q;    Q.push (Node (0,0)); while (!        Q.empty ()) {node now=q.top ();        Q.pop (); int S=now.        E for (int i=0;i<v[s].size (); i++) {if (Dist[s]+v[s][i]. W<dist[v[s][i]. E]) {dist[v[s][i]. E]=dist[s]+v[s][i].                W Q.push (Node (v[s][i). E,dist[v[s][i].            E]));    }}}}int Main () {int t,s,e,w,sum,ans;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);        V.clear (); V.resize (n+1);        sum=0;            while (m--) {scanf ("%d%d%d", &s,&e,&w);            V[s].push_back (Node (e,w));        V[e].push_back (Node (s,w));            } for (int i=1;i<=n;i++) {scanf ("%d", &pow[i]);        Sum+=pow[i];        } Dijikstra ();        for (int i=1;i<=sum;i++) Dp[i]=inf;        dp[0]=0; for (int i=1;i<=n;i++) {for (int j=sum;j>=pow[i];j--) {dp[j]=min (dp[j],d            P[j-pow[i]]+dist[i]);        }} Ans=inf;        for (int i=sum/2+1;i<=sum;i++) ans=min (Ans,dp[i]);        if (Ans==inf) cout<< "Impossible" <<endl;    else cout<<ans<<endl; } return 0;}

  

hdu3339 (dijkstra+ backpack)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.