Harmonious Army
Harmonious Army
Now, Bob is playing an interesting game in which he is a general of a harmonious army. There are n soldiers in this army. Each soldier should be in one of the two occupations, Mage or Warrior. There are m pairs of soldiers having combination ability. There are three kinds of combination ability. If the two soldiers in a pair are both Warriors, the army power would be increased by a. If the two soldiers in a pair are both Mages, the army power would be increased by c. Otherwise the army power would be increased by b, and b=a/4+c/3, guaranteed that 4|a and 3|c. Your task is to output the maximum power Bob can increase by arranging the soldiers' occupations.
Note that the symbol a|b means that a divides b, e.g. , 3|12 and 8|24.Input
There are multiple test cases.
Each case starts with a line containing two positive integers n(n≤500) and m(m≤\(10^4\)).
In the following m lines, each line contains five positive integersu,v,a,b,c (1≤u,v≤n,u≠v,1≤a,c≤4×\(10^6\),b=a/4+c/3), denoting soldiers u and v have combination ability, guaranteed that the pair (u,v) would not appear more than once.
It is guaranteed that the sum of n in all test cases is no larger than 5×\(10^3\), and the sum of m in all test cases is no larger than 5×\(10^4\).Output
For each test case, output one line containing the maximum power Bob can increase by arranging the soldiers' occupations.
Sample Input
3 2
1 2 8 3 3
2 3 4 3 6Sample Output
12
思路:
把\(s\)连向\(u,v\)连容量为\(\frac{a+b}{2}\)的有向边\(,\)把\(u,v\)向\(t\)连容量为\(\frac{b+c}{2}\)的有向边\(,\)在\(u,v\)间连条\(\frac{a+c}{2}-b\),用\(\sum_{i=1}^n(a_i+b_i+c_i)\)减最大流
如图:
证明:

(这张图找了半天)
其中一组解为
便得到了上面建边的由来
\(\mathfrak{Talk\ is\ cheap,show\ you\ the\ code.}\)
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
# define read read1<int>()
# define Type template<typename T>
Type inline T read1(){
T n=0;
char k;
bool fl=0;
do (k=getchar())=='-'&&(fl=1);while('9'<k||k<'0');
while(47<k&&k<58)n=(n<<3)+(n<<1)+(k^48),k=getchar();
return fl?-n:n;
}
class Flow{
# define Big 502
# define Big2 70000
# define Imax 2e9
private:
int cur[Big+1],q[Big+1],lev[Big+1];
class Pic{
public:
class Edge{
public:
int u,t;
double v;
}G[Big2<<1|1];
int f[Big+1],tot;
Pic(){for(int i=0;i<=Big;++i)f[i]=0;tot=0;}
void add(int u,int v,double t){
G[++tot].u=v;
G[tot].t=f[u];
f[u]=tot;
G[tot].v=t;
}
}G;
public:
int S,T,n;
void Clear(){memset(G.f,0,sizeof(G.f));G.tot=0;}
Flow(){n=Big;}
Flow(int big,int s,int t){n=big;S=s;T=t;}
void Into(int s,int t){S=s;T=t;}
void add(int u,int v,double t){
G.add(u,v,t);G.add(v,u,0);
}
bool bfs(){
for(int i=0;i++^n;lev[i]=0);
lev[S]=1;int t=0;
q[t++]=S;
for(int i=0;i^t;++i){
int u=q[i];
//printf("->%d\n",u);
for(int i=G.f[u];i;i=G.G[i].t){
if(!lev[G.G[i].u]&&G.G[i].v>0){
int to=G.G[i].u;
lev[to]=lev[u]+1;
q[t++]=to;
if(to==T)return 1;
}
}
}
return 0;
}
double dfs(int u,double f){
if(u==T)return f;
double tag=0,c;
for(int &i=cur[u];i;i=G.G[i].t){
int to=G.G[i].u;
if(G.G[i].v>0&&lev[to]==lev[u]+1){
c=dfs(to,min(f-tag,G.G[i].v));
G.G[i].v-=c;
G.G[(i-1^1)+1].v+=c;
tag+=c;
if(tag==f)return tag;
}
}
return tag;
}
double Dinic(){
double ans=0;
while(bfs()){
for(int i=0;i++^n;)cur[i]=G.f[i];
ans+=dfs(S,2e9);
}
return ans;
}
# undef Big
# undef Big2
}G;
# define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout)
# define f(i,l,r) for(int i=l;i<=r;++i)
int main(){
G.Into(1,2);
int n,m;
while(~scanf("%d %d",&n,&m)){
double t=0;
G.Clear();
for(int i=0;i++^m;){
int u=read,v=read;
double A=read,B=read,C=read;
t+=A+B+C;
G.add(1,u+2,(A+B)/2);
G.add(1,v+2,(A+B)/2);
G.add(u+2,2,(B+C)/2);
G.add(v+2,2,(B+C)/2);
G.add(u+2,v+2,(A+C)/2-B);
G.add(v+2,u+2,(A+C)/2-B);
}
printf("%.0f\n",t-G.Dinic());
}
return 0;
}
浮世苍茫,不过瞬逝幻梦
善恶爱诳,皆有定数
于命运之轮中
吞噬于黄泉之冥暗
呜呼,吾乃梦之戍人
幻恋之观者
唯于万华镜中,永世长存

浙公网安备 33010602011771号