本文共 2508 字,大约阅读时间需要 8 分钟。
//#pragma comment(linker, "/STACK:1024000000,1024000000")#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair pii;#define pb(a) push(a)#define INF 0x1f1f1f1f#define lson idx<<1,l,mid#define rson idx<<1|1,mid+1,r#define PI 3.1415926535898template T min(const T& a,const T& b,const T& c) { return min(min(a,b),min(a,c));}template T max(const T& a,const T& b,const T& c) { return max(max(a,b),max(a,c));}void debug() {#ifdef ONLINE_JUDGE#else freopen("in.txt","r",stdin); //freopen("d:\\out1.txt","w",stdout);#endif}int getch() { int ch; while((ch=getchar())!=EOF) { if(ch!=' '&&ch!='\n')return ch; } return EOF;}struct HeapNode{ int d,u; bool operator < (const HeapNode &ant) const { return d>ant.d; }};struct Edge{ int from,to; int dist;};const int maxn=41005;struct Dijksta{ int n; vector g[maxn]; vector edge; int done[maxn]; int d[maxn]; void init(int n) { this->n=n; for(int i=0;i<=n;i++) g[i].clear(); edge.clear(); } void add(int u,int v,int w) { Edge e=(Edge){u,v,w}; edge.push_back(e); g[u].push_back(edge.size()-1); } void solve(int s) { for(int i=0;i<=n;i++)d[i]=INF; memset(done,0,sizeof(done)); priority_queue q; q.push((HeapNode){ 0,s}); d[s]=0; while(!q.empty()) { HeapNode x=q.top();q.pop(); if(done[x.u])continue; int u=x.u; done[u]=1; for(int i=0;i A[j]) { b[i]=b[i]-(A[j]-b[j]); b[j]=A[j]; solver.add(ID(a[0],a[1]),ID(b[0],b[1]),A[j]-a[j]); }}void construct(){ for(a[0]=0;a[0]<=A[0];a[0]++) for(a[1]=0;a[1]+a[0]<=total&&a[1]<=A[1];a[1]++) { a[2]=total-a[0]-a[1]; pour(0,1);pour(0,2); pour(1,0);pour(1,2); pour(2,0);pour(2,1); }}void find(int &ans1,int &ans2){ ans1=INF; for(ans2=d;ans2>=0;ans2--) { for(int a=0;a<=A[0];a++) { for(int b=0;b+a<=total&&b<=A[1];b++) { int c=total-a-b; if((solver.d[ID(a,b)]!=INF)&&(a==ans2||b==ans2||c==ans2)) { ans1=min(ans1,solver.d[ID(a,b)]); } } } if(ans1!=INF)return ; }}int main(){ //freopen("in.txt","r",stdin); int t;scanf("%d",&t); for(int ca=1;ca<=t;ca++) { for(int i=0;i<3;i++)scanf("%d",&A[i]); scanf("%d",&d); total=A[2]; solver.init((A[0]+1)*(A[1]+1)); vcnt=0; memset(id,0,sizeof(id)); construct(); solver.solve(ID(0,0)); int ans1,ans2; find(ans1,ans2); printf("%d %d\n",ans1,ans2); } return 0;}
转载于:https://www.cnblogs.com/BMan/p/3647153.html