题目描述:
样例输入 1:
2
1 2 1 2 3
样例输出 1:
1 4
样例输入 2:
3
1 2 1 2 3
1 3 0 0 0
2 3 3 4 5
样例输出 2:
-1 -1
样例输入 3
4
1 2 0 2 1
2 3 0 2 1
1 3 0 2 6
1 4 0 0 1
2 4 0 0 0
3 4 2 3 0
样例输出 3:
2 15
样例输入 4
3
1 2 0 2 1
1 3 1 2 1
2 3 1 2 1
样例输出 4:
2 6
数据规模:
对于 30% 的数据:n≤3
对于 100% 的数据:1≤n≤6;1≤s< f≤n;0≤l< h≤5;0≤a≤6
题目分析:
既然题目都说了NOIP不能考网络流,那这道题就不是网络流了(网络流也没法做)。就是一个暴力dfs,注意细节。
附代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
int getint()
{
int i=0,f=1;char c;
for(c=getchar();(c<'0'||c>'9')&&c!='-';c=getchar());
if(c=='-')f=-1,c=getchar();
for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
return i*f;
}
const int INF=0x3f3f3f3f;
int n,m,ans,bz,ansflow=INF,ansmax,l[7][7],h[7][7],low[7],high[7],a[7][7],in[7],out[7];
void dfs(int x,int y,int flow,int pro)
{
if(flow>ansflow)return;
if(x==n)
{
bz=1;
if(flow<ansflow)ansflow=flow,ansmax=pro;
else if(flow==ansflow)ansmax=max(ansmax,pro);
return;
}
if(y>n)
{
dfs(x+1,x+2,flow,pro);
return;
}
if(x>1&&y==n)
{
int s=in[x]-out[x];
if(s>h[x][y]||s<l[x][y])return;
dfs(x,y+1,flow,pro+s*s+(s>0)*(a[x][y]));
return ;
}
for(int i=l[x][y];i<=h[x][y];i++)
{
out[x]+=i;in[y]+=i;
if(x==1)dfs(x,y+1,flow+i,pro+i*i+(i>0)*a[x][y]);
else dfs(x,y+1,flow,pro+i*i+(i>0)*a[x][y]);
out[x]-=i;in[y]-=i;
}
}
int main()
{
//freopen("flow.in","r",stdin);
//freopen("flow.out","w",stdout);
int x,y;
n=getint(),m=n*(n-1)/2;
while(m--)
{
x=getint(),y=getint();
l[x][y]=getint();
h[x][y]=getint();
a[x][y]=getint();
}
if(n==2)
{
if(l[1][2]>h[1][2])
{
cout<<-1<<" "<<-1;
return 0;
}
cout<<l[1][2]<<" "<<a[1][2]+l[1][2]*l[1][2];
return 0;
}
if(n==3)
{
low[1]=max(l[1][2],l[2][3])+l[1][3];
high[1]=min(h[1][2],h[2][3])+h[1][3];
if(l[1][2]>h[2][3]||h[1][2]<l[2][3])
{
cout<<-1<<" "<<-1;
return 0;
}
l[1][2]=max(l[1][2],l[2][3]);
if(l[1][2])ans+=a[1][2]+a[2][3]+2*l[1][2]*l[1][2];
if(l[1][3])ans+=a[1][3]+l[1][3]*l[1][3];
cout<<low[1]<<" "<<ans;
return 0;
}
dfs(1,2,0,0);
if(bz)cout<<ansflow<<" "<<ansmax;
else cout<<-1<<" "<<-1;
return 0;
}