# 2236. 伊基的故事 I – 道路重建()-其他

## 2236. 伊基的故事 I – 道路重建()

### 数据范围

\(1 \le N \le 500\),
\(1 \le M \le 5000\),
\(0 \le a,b < N\),
\(0 \le c \le 100\)

### 输入样例：

``````2 1
0 1 1
``````

### 输出样例：

``````1
``````

### 解题思路

``dfs``
• 时间复杂度：\(O(n^2m)\)

### 代码

``````// Problem: 伊基的故事 I - 道路重建
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/2238/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
//

// %%%Skyqwq
#include <bits/stdc++.h>

//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }

template <typename T> void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}

const int N=505,M=5005*2,inf=1e9;
int n,m,S,T;
int h[N],e[M],f[M],ne[M],idx;
bool st_s[N],st_t[N];
int d[N],q[N],hh,tt,cur[N];
bool bfs()
{
memset(d,-1,sizeof d);
q[0]=S;
d[S]=hh=tt=0;
cur[S]=h[S];
while(hh<=tt)
{
int x=q[hh++];
for(int i=h[x];~i;i=ne[i])
{
int y=e[i];
if(d[y]==-1&&f[i])
{
d[y]=d[x]+1;
cur[y]=h[y];
if(y==T)return true;
q[++tt]=y;
}
}
}
return false;
}
int dfs(int x,int limit)
{
if(x==T)return true;
int flow=0;
for(int i=cur[x];~i&&flow<limit;i=ne[i])
{
cur[x]=i;
int y=e[i];
if(d[y]==d[x]+1&&f[i])
{
int t=dfs(y,min(f[i],limit-flow));
if(!t)d[y]=-1;
f[i]-=t,f[i^1]+=t,flow+=t;
}
}
return flow;
}
int dinic()
{
int res=0,flow;
while(bfs())while(flow=dfs(S,inf))res+=flow;
return res;
}
{
e[idx]=b,f[idx]=c,ne[idx]=h[a],h[a]=idx++;
e[idx]=a,f[idx]=0,ne[idx]=h[b],h[b]=idx++;
}
void dfs(bool st[],int x,int t)
{
st[x]=true;
for(int i=h[x];~i;i=ne[i])
if(!st[e[i]]&&f[i^t])dfs(st,e[i],t);
}
int main()
{
memset(h,-1,sizeof h);
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
}
S=0,T=n-1;
dinic();
dfs(st_s,S,0),dfs(st_t,T,1);
int res=0;
for(int i=0;i<idx;i+=2)
if(!f[i]&&st_t[e[i]]&&st_s[e[i^1]])res++;
printf("%d",res);
return 0;
}
``````
————————

### 数据范围

\(1 \le N \le 500\),
\(1 \le M \le 5000\),
\(0 \le a,b < N\),
\(0 \le c \le 100\)

### 输入样例：

``````2 1
0 1 1
``````

### 输出样例：

``````1
``````

### 解题思路

``dfs``
• 时间复杂度：\(O(n^2m)\)

### 代码

``````// Problem: 伊基的故事 I - 道路重建
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/2238/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
//

// %%%Skyqwq
#include <bits/stdc++.h>

//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }

template <typename T> void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}

const int N=505,M=5005*2,inf=1e9;
int n,m,S,T;
int h[N],e[M],f[M],ne[M],idx;
bool st_s[N],st_t[N];
int d[N],q[N],hh,tt,cur[N];
bool bfs()
{
memset(d,-1,sizeof d);
q[0]=S;
d[S]=hh=tt=0;
cur[S]=h[S];
while(hh<=tt)
{
int x=q[hh++];
for(int i=h[x];~i;i=ne[i])
{
int y=e[i];
if(d[y]==-1&&f[i])
{
d[y]=d[x]+1;
cur[y]=h[y];
if(y==T)return true;
q[++tt]=y;
}
}
}
return false;
}
int dfs(int x,int limit)
{
if(x==T)return true;
int flow=0;
for(int i=cur[x];~i&&flow<limit;i=ne[i])
{
cur[x]=i;
int y=e[i];
if(d[y]==d[x]+1&&f[i])
{
int t=dfs(y,min(f[i],limit-flow));
if(!t)d[y]=-1;
f[i]-=t,f[i^1]+=t,flow+=t;
}
}
return flow;
}
int dinic()
{
int res=0,flow;
while(bfs())while(flow=dfs(S,inf))res+=flow;
return res;
}
{
e[idx]=b,f[idx]=c,ne[idx]=h[a],h[a]=idx++;
e[idx]=a,f[idx]=0,ne[idx]=h[b],h[b]=idx++;
}
void dfs(bool st[],int x,int t)
{
st[x]=true;
for(int i=h[x];~i;i=ne[i])
if(!st[e[i]]&&f[i^t])dfs(st,e[i],t);
}
int main()
{
memset(h,-1,sizeof h);
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);