[SDOI2010]星际竞速

昨天连续做了两道网络流……这是第一道


这是道图论题是肯定的,图都给你了

那么问题在于如何建模

问题要求访问每个点恰好一次(我一开始没看到这个条件……)

要求总时间最短,尝试把问题转化为一些经典图论问题比如最短路

很可惜不行,那么自然想到网络流(组里面有句戏言叫“一切皆可网络流”,比如A+B……)

进一步分析发现单纯的网络流是不行的,需要用费用流

访问每个点恰好一次,跟路径覆盖其实有点像……

把每个星球拆成两个点,u和u'

我们对每条题目给定的边(u,v),在网络流中加一条边(u,v'),流量为1,费用为时间

然后第一次可以前往任何一个点,那么从st向v'连一条边,流量为1,费用为定位时间

从每个v'向ed连一条边,容量为1,费用为0,表示每个点的的入度为1,仅会访问一次

从st向每个u连一条边,容量为1,费用为0,以便u通过(u,v')到达v'

那么这个图的最小费用流就是答案


至今只会写EK版的费用流……速度超慢……傻×一个……

//Lib #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<ctime> #include<iostream> #include<algorithm> #include<vector> #include<string> #include<queue> using namespace std; //Macro #define rep(i,a,b) for(int i=a,tt=b;i<=tt;++i) #define drep(i,a,b) for(int i=a,tt=b;i>=tt;--i) #define erep(i,e,x) for(int i=x;i;i=e[i].next) #define irep(i,x) for(__typedef(x.begin()) i=x.begin();i!=x.end();i++) #define read() (strtol(ipos,&ipos,10)) #define sqr(x) ((x)*(x)) #define pb push_back #define PS system("pause"); typedef long long ll; typedef pair<int,int> pii; const int oo=~0U>>1; const double inf=1e100; const double eps=1e-6; string name="", in=".in", out=".out"; //Var struct E { int next,node,cap,v; }e[40008]; queue<int> q; int n,m,st,ed,ans,tot=1; int h[1608],dis[1608],pre[1608],epre[1608]; bool vis[1608]; void add(int a,int b,int c,int d) { e[++tot].next=h[a];e[tot].node=b;e[tot].cap=c;e[tot].v=d;h[a]=tot; e[++tot].next=h[b];e[tot].node=a;e[tot].cap=0;e[tot].v=-d;h[b]=tot; } void Init() { scanf("%d%d",&n,&m); st=n*2+1;ed=n*2+2; int a,b,c; rep(i,1,n) { scanf("%d",&c); // add(st,i,1,0); add(st,n+i,1,c); add(n+i,ed,1,0); } rep(i,1,m) { scanf("%d%d%d",&a,&b,&c); if(a>b)swap(a,b); add(a,b+n,1,c); } } bool SPFA() { memset(dis,55,sizeof dis); q.push(st);dis[st]=0; int u,v;bool flag=false; while(!q.empty()) { u=q.front();q.pop();vis[u]=false; erep(i,e,h[u]) if(e[i].cap&&dis[v=e[i].node]>dis[u]+e[i].v) { dis[v]=dis[u]+e[i].v; pre[v]=u;epre[v]=i; if(!vis[v])q.push(v),vis[v]=true; if(v==ed)flag=true; } } return flag; } int Flow() { int tmp,ret=0,flow=oo; tmp=ed; while(tmp!=st) { flow=min(flow,e[epre[tmp]].cap); ret+=e[epre[tmp]].v; tmp=pre[tmp]; } tmp=ed; while(tmp!=st) { e[epre[tmp]].cap-=flow; e[epre[tmp]^1].cap+=flow; tmp=pre[tmp]; } return ret*flow; } void Work() { while(SPFA()) ans+=Flow(); printf("%d\n",ans); } int main() { // freopen((name+in).c_str(),"r",stdin); // freopen((name+out).c_str(),"w",stdout); Init(); Work(); return 0; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值