2019暑假杭电多校赛第一场hdu6582
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.
Input
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≤&10^9$), denoting that there exists a one-way road from the house indexed x to y of length c.
Output
Print T lines, each line containing a integer, the answer.
Sample Input
1
3 4
1 2 1
2 3 1
1 3 2
1 3 3
Sample Output
3
题目大意:
a从1要到n点找b,但c不想让a那么容易找到b,所以c会拦住一些边,让a走到n点消耗最多,问你c拦路的最小消耗是多少。
题目思路:
最小割模板题。
最大流等于最小割。
复杂度:最短路+网络流
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pa pair<ll,ll>
using namespace std;
const ll maxn=1e4+5;
const ll maxm=1e7+10;
const ll mod=1e9+7;
vector<pair<int,int>>ve[maxn];
ll dis[maxn];
int n,m,cnt=-1,head[maxn],dep[maxn];
bool vis[maxn];
struct node{
int to,next,v;
}edge[maxn*2];//maxn=1e4
void add_edge(int a,int b,int c){//边数从0开始,每次加上反向边,这样奇偶交替加边,重边无所谓,存图
edge[++cnt].to=b;
edge[cnt].next=head[a];
edge[cnt].v=c;
head[a]=cnt;
}
bool bfs(){ //分层图
for(int i=1;i<=n;i++)dep[i]=1e9;
dep[1]=0;
queue<int>que;
que.push(1);
while(que.size()){
int now=que.front();
que.pop();
for(int i=head[now];i!=-1;i=edge[i].next){
int to=edge[i].to;
int v=edge[i].v;
if(v&&dep[to]>dep[now]+1){
dep[to]=dep[now]+1;
que.push(to);
}
}
}
if(dep[n]==1e9)return false;
else return true;
}
int dfs(int x,int lowflow){ //找最短增广路
if(x==n||lowflow==0)return lowflow;
int reslow=0;
int used=0;
for(int i=head[x];i!=-1;i=edge[i].next){
int to=edge[i].to;
if(edge[i].v&&dep[x]+1==dep[to]){
if(reslow=dfs(to,min(lowflow,edge[i].v))){
edge[i].v-=reslow;
edge[i^1].v+=reslow;
used+=reslow;
lowflow-=reslow;
if(lowflow<=0)break;
}
}
}
return used;
}
void Dinic(){
ll maxflow=0;
int lowflow;
while(bfs()){ //找增广路的分层图
while(lowflow=dfs(1,1e9)){ //若return 0则说明找不到最短增广路,则重新跑一下bfs,找增长的增广路。
maxflow+=lowflow; //最大流=各增广路的流相加
}
}
printf("%lld\n",maxflow);
}
void dijkstra(){ //优先队列实现dijkstra
for(int i=1;i<=n;i++)dis[i]=1e18,vis[i]=false;
dis[1]=0;
priority_queue<pair<ll,int>>que;
que.push(make_pair(0,1));
while(que.size()){
pair<ll,int>now=que.top();
que.pop();
if(vis[now.second])continue;
vis[now.second]=true;
for(int i=0;i<ve[now.second].size();i++){
int to=ve[now.second][i].first;
int w=ve[now.second][i].second;
if(dis[to]>dis[now.second]+w){
dis[to]=dis[now.second]+w;
que.push(make_pair(-dis[to],to));
}
}
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++){
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
ve[a].push_back(make_pair(b,c));
}
dijkstra();
if(dis[n]==1e18){ //判断最先是否可以到达n点
printf("0\n");
continue;
}
for(int i=1;i<=n;i++)head[i]=-1;
cnt=-1;
for(int i=1;i<=n;i++){
for(int j=0;j<ve[i].size();j++){
int a=i,b=ve[i][j].first,c=ve[i][j].second;
if(dis[a]+c==dis[b])add_edge(a,b,c),add_edge(b,a,0); //存入有向图和反向残存图
}
}
Dinic();
for(int i=1;i<=n;i++)ve[i].clear();
}
return 0;
}