夏夜、

心若平似镜、何题不AC。

UVA 10603 Fill 最短路

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> 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.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
    return min(min(a,b),min(a,c));
}
template<class T> 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<int> g[maxn];
    vector<Edge> 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<HeapNode> 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<g[u].size();i++)
            {
                Edge &e=edge[g[u][i]];
                if(d[u]+e.dist<d[e.to])
                {
                    d[e.to]=d[u]+e.dist;
                    q.push((HeapNode){d[e.to],e.to});
                }
            }
        }
    }
};

Dijksta solver;
int d;
int A[3],a[3];
int total;
int id[205][205];
int vcnt;
int ID(int a,int b)
{
    int &x=id[a][b];
    if(x==0)x=++vcnt;
    return x;
}

void pour(int i,int j)
{
    int b[3];
    memcpy(b,a,sizeof(a));
    if(b[i]!=0&&b[i]+b[j]<=A[j])
    {
        b[j]+=b[i];
        b[i]=0;
        solver.add(ID(a[0],a[1]),ID(b[0],b[1]),a[i]);
    }
    memcpy(b,a,sizeof(a));
    if(b[i]!=0&&b[i]+b[j]>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;
}
View Code

 

posted on 2014-04-05 13:28  BMan、  阅读(153)  评论(0编辑  收藏  举报

导航