最大流模板题
PATH
题目描述
Years later, Jerry fell in love with a girl, and he often walks for a long time to pay visits to her. But, because he spends too much time with his girlfriend, Tom feels neglected and wants to prevent him from visiting her.
After doing some research on the neighbourhood, Tom found that the neighbourhood consists of exactly n houses, and some of them are connected with directed road. To visit his girlfriend, Jerry needs to start from his house indexed 1 and go along the shortest path to hers, indexed n.
Now Tom wants to block some of the roads so that Jerry has to walk longer to reach his girl’s home, and he found that the cost of blocking a road equals to its length. Now he wants to know the minimum total cost to make Jerry walk longer.
Note, if Jerry can’t reach his girl’s house in the very beginning, the answer is obviously zero. And you don’t need to guarantee that there still exists a way from Jerry’s house to his girl’s after blocking some edges.
输入
The input begins with a line containing one integer T(1≤T≤10), the number of test cases.
Each test case starts with a line containing two numbers n,m(1≤n,m≤10000), the number of houses and the number of one-way roads in the neighbourhood.
m lines follow, each of which consists of three integers x,y,c(1≤x,y≤n,1≤c≤109), denoting that there exists a one-way road from the house indexed x to y of length c.
输出
Print T lines, each line containing a integer, the answer.
样例输入
复制样例数据
1
3 4
1 2 1
2 3 1
1 3 2
1 3 3
样例输出
3
题意是让最短路不能走花费的最小代价。先跑一遍最短路。然后我也不会了,套的模板
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int n,m,s,t;
typedef long long ll;
const int N =100001;
ll dist[N],inf=ll(1e18);
bool inque[N];
struct edge
{
int v;
int time;
};
struct Edge{
int to,rev;
ll value;
Edge() {}
Edge(int a,ll b,int c):to(a),value(b),rev(c) {}
};
struct youxian{
int index;
ll dist;
bool operator<(const youxian &tmp)
const {
return dist>tmp.dist;
}
};
vector<edge>e[N];
void Dijkstra()
{
int i;
for(i=1;i<=n;i++)
{
dist[i]=inf;
inque[i]=false;
}
dist[s]=0;
priority_queue<youxian>q;
q.push({s,0});
while(!q.empty()){
int u=q.top().index;
q.pop();
if(inque[u]) continue;
inque[u]=true;
int size=int (e[u].size());
for(i=0;i<size;i++){
int v=e[u][i].v,time=e[u][i].time;
if(dist[v]>dist[u]+time){
dist[v]=dist[u]+time;
q.push({v,dist[v]});
}
}
}
}
vector<Edge>E[N];
int deep[N];
int iter[N];
void add(int from,int to,ll value){
E[from].push_back(Edge(to,value,E[to].size()));
E[to].push_back(Edge(from,0,E[from].size()-1));
}
bool BFS(int root,int target) {
memset(deep,-1,sizeof deep);
queue<int> Q;
deep[root] = 0;
Q.push(root);
while(!Q.empty())
{
int t = Q.front();
Q.pop();
for(int i=0 ; i<E[t].size() ; i++)
{
if(E[t][i].value > 0 && deep[E[t][i].to] == -1)
{
deep[E[t][i].to] = deep[t] + 1;
Q.push(E[t][i].to);
}
}
}
return deep[target] != -1;
}
ll DFS(int root,int target,ll flow){
if(root == target)return flow;
for(int &i=iter[root] ; i<E[root].size() ; i++)
{
if(E[root][i].value>0 && deep[E[root][i].to] == deep[root]+1)
{
ll nowflow = DFS(E[root][i].to,target,min(flow,E[root][i].value));
if(nowflow > 0)
{
E[root][i].value -= nowflow;
E[E[root][i].to][E[root][i].rev].value += nowflow;
return nowflow;
}
}
}
return 0;
}
ll Dinic(int root,int target){
ll sumflow = 0;
while(BFS(root,target)) {
memset(iter,0,sizeof iter);
ll mid;
while((mid=DFS(root,target,inf)) > 0)sumflow += mid;
}
return sumflow;
}
int main()
{
int num;
cin>>num;
while(num--){
int u,v,w;
scanf("%d%d",&n,&m);
s=1;
t=n;
for(int i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
e[u].push_back({v,w});
//e[v].push_back({u,w});
}
Dijkstra();
if(dist[n]==inf){
printf("0\n");
}
else {
for(int i=1;i<=n;++i){
for(int j=0;j<e[i].size();++j){
int to=e[i][j].v;
ll val=e[i][j].time;
if(dist[to]-dist[i]==val){
add(i,to,val);
}
}
}
printf("%lld\n",Dinic(1,n));
for (int i=0;i<=11000;i++) {
E[i].clear();
e[i].clear();
}
}
}
return 0;
}