[BZOJ1927][SDOI2010]星际竞速 最小费用最大流

来源:互联网 发布:凯立德导航更改端口 编辑:程序博客网 时间:2024/04/29 20:05

用流量限制次数,用费用限制距离。
这一题和最小路径覆盖很像,所有边容量为1,把每个点拆成uu,原图上uv连成uv,费用为距离,每个u连向汇点,费用0,源点连向每个u,费用0,源点连向每个u,费用为瞬移代价,直接跑最小费用最大流就可以了。
注意最小费用最大流每个点记的是来边而不是来点。

#include<ctime>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<cassert>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<sstream>#include<climits>#define X first#define Y second#define DB double#define lc now<<1#define rc now<<1|1#define MP make_pair#define LL long long#define pb push_back#define sqr(_) ((_)*(_))#define INF 0x3f3f3f3f#define pii pair<int,int>#define pdd pair<DB,DB>#define ull unsigned LL#define DEBUG(...) fprintf(stderr,__VA_ARGS__)using namespace std;const int MAXN=2000;template<typename T>void Read(T& x){    x=0;int flag=0,sgn=1;char c;    while(c=getchar())    {        if(c=='-')sgn=-1;        else if(c>='0'&&c<='9')x*=10,x+=c-'0',flag=1;        else if(flag)break;    }    x*=sgn;}int w[MAXN],n,m;struct Edge{    int u,v,cap,flow,cost;    Edge(int u=0,int v=0,int cap=0,int flow=0,int cost=0):        u(u),v(v),cap(cap),flow(flow),cost(cost){}};struct Mincost{    vector<int> G[MAXN];    vector<Edge> edges;    int p[MAXN],inq[MAXN],d[MAXN],a[MAXN],S,T;    void add(int u,int v,int cap,int cost)    {        edges.pb(Edge(u,v,cap,0,cost));        edges.pb(Edge(v,u,0,0,-cost));        int k=edges.size();        G[u].pb(k-2);        G[v].pb(k-1);    }    void init()    {        memset(p,0,sizeof(p));        memset(inq,0,sizeof(inq));        memset(a,0,sizeof(a));        memset(d,INF,sizeof(d));    }    bool SPFA(int& flow,int& cost)    {        init();        queue<int> q;        q.push(S);        inq[S]=1;        d[S]=0;        a[S]=INF;        while(!q.empty())        {            int u=q.front();q.pop();            inq[u]=0;            for(int i=0;i<G[u].size();i++)            {                Edge& e=edges[G[u][i]];                if(d[e.v]>d[u]+e.cost&&e.cap>e.flow)                {                    d[e.v]=d[u]+e.cost;                    p[e.v]=G[u][i];                    a[e.v]=min(a[u],e.cap-e.flow);                    if(!inq[e.v])                    {                        q.push(e.v);                        inq[e.v]=1;                    }                }            }        }        if(d[T]==INF)return 0;        flow+=a[T];        cost+=d[T]*a[T];        int u=T;        while(u!=S)        {            edges[p[u]].flow+=a[T];            edges[p[u]^1].flow-=a[T];            u=edges[p[u]].u;        }        return 1;    }    int mincost()    {        int flow=0,cost=0;        while(SPFA(flow,cost));        return cost;    }}Graph;int main(){#ifndef ONLINE_JUDGE    freopen("speed.in","r",stdin);    freopen("speed.out","w",stdout);#endif    Read(n);Read(m);    for(int i=1;i<=n;i++)        Read(w[i]);    for(int i=1;i<=m;i++)    {        int a,b,c;        Read(a),Read(b),Read(c);        if(a>b)swap(a,b);        Graph.add(a,b+n,1,c);    }    Graph.S=2*n+1,Graph.T=2*n+2;    for(int i=1;i<=n;i++)    {        Graph.add(Graph.S,i,1,0);        Graph.add(i+n,Graph.T,1,0);        Graph.add(Graph.S,i+n,1,w[i]);    }    cout<<Graph.mincost()<<endl;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果手机成耳机模式了怎么办 华为mate8耳机声音小怎么办 移动sim卡丢了怎么办 蓝牙耳机开不开机怎么办 苹果手机蓝牙不匹配怎么办 苹果6蓝牙坏了怎么办 蓝牙密钥不正确不匹配怎么办 华为p6开不了机怎么办 华为c199手机不停重启怎么办 华为手机用户数据被锁定怎么办 朵唯手机丢了怎么办 网件r6220穿墙差怎么办 无线网打王者卡怎么办 酷翼x9忘了密码怎么办 楼上的路由器楼下不好使怎么办 电信4g网络不好怎么办 农村只有2g网怎么办 电信卡4g网速慢怎么办 小米手机触屏失灵怎么办 荣耀v10电信网速很慢怎么办 华为路由器掉线了怎么办 三星s6只识别一张卡怎么办 华为手机卡不显示了怎么办 华为账号手机卡丢了怎么办 荣耀8耗电量太快怎么办 vivo卡2不显示怎么办 电信宽带玩王者荣耀卡怎么办 联通4g玩游戏卡怎么办 华为悦盒遥控器丢了怎么办 6s不能用电信卡怎么办 iphone6电信卡无服务怎么办 魅族手机电信卡怎么办 小米手机读不出sim卡怎么办 魅蓝note6耗电快怎么办 oppo手机下载密码忘了怎么办 华为v9玩飞车卡怎么办 苹果7耳机转换器不支持怎么办 华为mate10耳机声音小怎么办 200打一年到期了怎么办 手机欠费变成2g怎么办 手机4g网用不了怎么办