Harry Potter and the Forbidden Forest
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 158 Accepted Submission(s): 56
The Forbidden Forest is mysterious. It consists of N nodes numbered from 0 to N-1. All of Death Eaters stay in the node numbered 0. The position of Castle is node n-1. The nodes connected by some roads. Harry need block some roads by magic and he want to minimize the cost. But it’s not enough, Harry want to know how many roads are blocked at least.
The first line is number of test case.
Each test case, the first line contains two integers n, m, which means the number of nodes and edges of the graph. Each node is numbered 0 to n-1.
Following m lines contains information about edges. Each line has four integers u, v, c, d. The first two integers mean two endpoints of the edges. The third one is cost of block the edge. The fourth one means directed (d = 0) or undirected (d = 1).
Technical Specification
1. 2 <= n <= 1000
2. 0 <= m <= 100000
3. 0 <= u, v <= n-1
4. 0 < c <= 1000000
5. 0 <= d <= 1
Output the case number and the answer of how many roads are blocked at least.
0 1 3 0
0 2 1 0
1 2 1 1
1 3 1 1
2 3 3 16 7
0 1 1 0
0 2 1 0
0 3 1 0
1 4 1 0
2 4 1 0
3 5 1 0
4 5 2 03 6
0 1 1 0
0 1 2 0
1 1 1 1
1 2 1 0
1 2 1 0
2 1 1 1
Case 2: 2
Case 3: 2
#include<cstdio>
#include<cstring>
#define N 1005
#define M 400005
#define inf 1ll<<60
#define min(a,b) ((a)<(b)?(a):(b))
int n,m,s,t,num,adj[N],dis[N],q[N];
struct edge
{
__int64 u,v,w,c,pre;
}e[M];
void insert1(__int64 u,__int64 v,__int64 w)
{
e[num]=(edge){u,v,w,w,adj[u]};
adj[u]=num++;
e[num]=(edge){v,u,0,w,adj[v]};//有向图
adj[v]=num++;
}
void insert2(__int64 u,__int64 v,__int64 w)
{
e[num]=(edge){u,v,w,w,adj[u]};
adj[u]=num++;
e[num]=(edge){v,u,w,w,adj[v]};//无向图
adj[v]=num++;
}
int bfs()
{
int i,x,v,head=0,tail=0;
memset(dis,0,sizeof(dis));
dis[s]=1;
q[tail++]=s;
while(head<tail)
{
x=q[head++];
for(i=adj[x];~i;i=e[i].pre)
if(e[i].w&&!dis[v=e[i].v])
{
dis[v]=dis[x]+1;
if(v==t)
return 1;
q[tail++]=v;
}
}
return 0;
}
__int64 dfs(__int64 x,__int64 limit)
{
if(x==t)
return limit;
__int64 i,v,tmp,cost=0;
for(i=adj[x];~i&&cost<limit;i=e[i].pre)
if(e[i].w&&dis[x]==dis[v=e[i].v]-1)
{
tmp=dfs(v,min(limit-cost,e[i].w));
if(tmp)
{
e[i].w-=tmp;
e[i^1].w+=tmp;
cost+=tmp;
}
else
dis[v]=-1;
}
return cost;
}
__int64 Dinic()
{
__int64 ans=0;
while(bfs())
ans+=dfs(s,inf);
return ans;
}
int main()
{
int tt=0,cc;
scanf("%d",&cc);
while(cc--)
{
scanf("%d%d",&n,&m);
int i,u,v,d;
__int64 w,c,ans;
memset(adj,-1,sizeof(adj));
num=0;
s=0;
t=n-1;
for(i=0;i<m;i++)
{
scanf("%d%d%I64d%d",&u,&v,&c,&d);
w=1+(m+1)*c;
if(d)
insert2(u,v,w);//无向边
else
insert1(u,v,w);//有向边
}
ans=Dinic();//maxflow=ans/(m+1);
printf("Case %d: %I64d\n",++tt,ans%(m+1));
}
}
4万+




被折叠的 条评论
为什么被折叠?



