【BZOJ】【1520】【POI2006】Szk-Schools
网络流/费用流
比较裸的一道题
依旧是二分图模型,由源点S连向每个学校 i (1,0),「注意是连向第 i 所学校,不是连向学校的标号m[i]……唉这里WA了一次」
然后对于每所学校 i 连接 j+n (a[i]≤j≤b[i]) 流量为1,费用为 abs(m[i]-j)*k[i] ,最后对于每个标号 j 连边 j+n->T 流量为1费用为0。
跑完费用流以后看流量是否为n,如果不是就说明无解。

1 /************************************************************** 2 Problem: 1520 3 User: Tunix 4 Language: C++ 5 Result: Accepted 6 Time:1452 ms 7 Memory:5964 kb 8 ****************************************************************/ 9 10 //BZOJ 1520 11 #include<vector> 12 #include<cstdio> 13 #include<cstring> 14 #include<cstdlib> 15 #include<iostream> 16 #include<algorithm> 17 #define rep(i,n) for(int i=0;i<n;++i) 18 #define F(i,j,n) for(int i=j;i<=n;++i) 19 #define D(i,j,n) for(int i=j;i>=n;--i) 20 #define pb push_back 21 using namespace std; 22 inline int getint(){ 23 int v=0,sign=1; char ch=getchar(); 24 while(ch<'0'||ch>'9'){ if (ch=='-') sign=-1; ch=getchar();} 25 while(ch>='0'&&ch<='9'){ v=v*10+ch-'0'; ch=getchar();} 26 return v*sign; 27 } 28 const int N=410,M=200000,INF=~0u>>2; 29 typedef long long LL; 30 /******************tamplate*********************/ 31 int n,ans,flow; 32 struct edge{int from,to,v,c;}; 33 struct Net{ 34 edge E[M]; 35 int head[N],next[M],cnt; 36 void ins(int x,int y,int z,int c){ 37 E[++cnt]=(edge){x,y,z,c}; 38 next[cnt]=head[x]; head[x]=cnt; 39 } 40 void add(int x,int y,int z,int c){ 41 ins(x,y,z,c); ins(y,x,0,-c); 42 } 43 int from[N],Q[M],d[N],S,T,ed; 44 bool inq[N],sign; 45 bool spfa(){ 46 int l=0,r=-1; 47 F(i,1,T) d[i]=INF; 48 d[S]=0; Q[++r]=S; inq[S]=1; 49 while(l<=r){ 50 int x=Q[l++]; 51 inq[x]=0; 52 for(int i=head[x];i;i=next[i]) 53 if(E[i].v>0 && d[x]+E[i].c<d[E[i].to]){ 54 d[E[i].to]=d[x]+E[i].c; 55 from[E[i].to]=i; 56 if (!inq[E[i].to]){ 57 Q[++r]=E[i].to; 58 inq[E[i].to]=1; 59 } 60 } 61 } 62 return d[T]!=INF; 63 } 64 void mcf(){ 65 int x=INF; 66 for(int i=from[T];i;i=from[E[i].from]) 67 x=min(x,E[i].v); 68 for(int i=from[T];i;i=from[E[i].from]){ 69 E[i].v-=x; 70 E[i^1].v+=x; 71 } 72 flow+=x; 73 ans+=x*d[T]; 74 } 75 void init(){ 76 n=getint(); cnt=1; 77 S=0; T=n*2+1; 78 int m,x,y,k; 79 F(i,1,n){ 80 m=getint(); x=getint(); y=getint(); k=getint(); 81 add(S,i,1,0); 82 F(j,x,y) add(i,j+n,1,k*abs(j-m)); 83 add(i+n,T,1,0); 84 } 85 while(spfa()) mcf(); 86 if (flow==n) printf("%d\n",ans); 87 else printf("NIE"); 88 } 89 }G1; 90 91 int main(){ 92 #ifndef ONLINE_JUDGE 93 freopen("1520.in","r",stdin); 94 freopen("1520.out","w",stdout); 95 #endif 96 G1.init(); 97 return 0; 98 }
1520: [POI2006]Szk-Schools
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 428 Solved: 220
[Submit][Status][Discuss]
Description

Input

Output
如果有可行解, 输出最小代价,否则输出NIE.
Sample Input
5
1 1 2 3
1 1 5 1
3 2 5 5
4 1 5 10
3 3 3 1
1 1 2 3
1 1 5 1
3 2 5 5
4 1 5 10
3 3 3 1
Sample Output
9
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术