Path(HDU-6582)
Problem Description
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≤109), 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 3Sample Output
3
题意:t 组样例,每组给出 n 个点 m 条有向边,现在有个人要从 1 号点走到 n 号点,可以删除一条路,问不让这个人从 1 号点到 n 个号点走最短路的的最小代价
思路:直接跑出最短路,然后将最短路加进网络流图,跑 Dinic 求一个最小割即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL quickModPow(LL a,LL b,LL mod){ LL res=1; a=a%mod; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1;} return res; }
LL getInv(LL a,LL mod){ return quickModPow(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 20000+5;
const int dx[] = {-1,1,0,0,1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
struct Edge_SPFA{
int to,next;
LL dis;
}edge_SPFA[N<<1];
int head_SPFA[N],tot_SPFA;
void addEdge_SPFA(int x,int y,LL dis){
edge_SPFA[++tot_SPFA].to=y;
edge_SPFA[tot_SPFA].dis=dis;
edge_SPFA[tot_SPFA].next=head_SPFA[x];
head_SPFA[x]=tot_SPFA;
}
bool vis_SPFA[N];
LL dis_SPFA[N];
void SPFA(LL s){
memset(vis_SPFA,false,sizeof(vis_SPFA));
memset(dis_SPFA,INF,sizeof(dis_SPFA));
vis_SPFA[s]=true;
dis_SPFA[s]=0;
queue<LL> Q;
Q.push(s);
while(!Q.empty()){
LL x=Q.front();
Q.pop();
vis_SPFA[x]=false;
for(int i=head_SPFA[x];i!=-1;i=edge_SPFA[i].next){
int y=edge_SPFA[i].to;
LL dis=edge_SPFA[i].dis;
if(dis_SPFA[y]>dis_SPFA[x]+dis){
dis_SPFA[y]=dis_SPFA[x]+dis;
if(!vis_SPFA[y]){
vis_SPFA[y]=true;
Q.push(y);
}
}
}
}
}
struct Edge_Dinic{
int from,to;
LL cap,flow;
Edge_Dinic(){}
Edge_Dinic(int from,int to,LL cap,LL flow):from(from),to(to),cap(cap),flow(flow){}
};
int S,T;
vector<Edge_Dinic> edge_Dinic;
vector<LL> G_Dinic[N];
bool vis_Dinic[N];
LL dis_Dinic[N];
LL cur_Dinic[N];
void addEdge_Dinic(int from,int to,LL cap){
edge_Dinic.push_back( Edge_Dinic(from,to,cap,0) );
edge_Dinic.push_back( Edge_Dinic(to,from,0,0) );
LL m=edge_Dinic.size();
G_Dinic[from].push_back(m-2);
G_Dinic[to].push_back(m-1);
}
bool BFS(){//构建层次网络
memset(vis_Dinic,0,sizeof(vis_Dinic));
dis_Dinic[S]=0;
vis_Dinic[S]=true;
queue<LL> Q;//用来保存节点编号
Q.push(S);
while(!Q.empty()){
LL x=Q.front();
Q.pop();
for(LL y=0;y<G_Dinic[x].size();y++){
Edge_Dinic &e=edge_Dinic[G_Dinic[x][y]];
if(!vis_Dinic[e.to] && e.cap>e.flow){
vis_Dinic[e.to]=true;
dis_Dinic[e.to]=dis_Dinic[x]+1;
Q.push(e.to);
}
}
}
return vis_Dinic[T];
}
LL DFS(int x,LL cp){//cp表示从s到x目前为止所有弧的最小残量
if(x==T || cp==0)
return cp;
LL flow=0,newFlow;//flow用来记录从x到t的最小残量
for(LL &y=cur_Dinic[x];y<G_Dinic[x].size();y++){
Edge_Dinic &e=edge_Dinic[G_Dinic[x][y]];
if(dis_Dinic[x]+1==dis_Dinic[e.to]){
LL minn=min(cp,e.cap-e.flow);
newFlow=DFS(e.to,minn);
if(newFlow>0){
e.flow+=newFlow;
edge_Dinic[G_Dinic[x][y]^1].flow-=newFlow;
flow+=newFlow;
cp-=newFlow;
if(cp==0)
break;
}
}
}
return flow;
}
LL Dinic(){
LL flow=0;
while(BFS()){
memset(cur_Dinic,0,sizeof(cur_Dinic));
flow+=DFS(S,INF);
}
return flow;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
for(int i=0;i<N;i++)
G_Dinic[i].clear();
memset(head_SPFA,-1,sizeof(head_SPFA));
tot_SPFA=0;
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int x,y;
LL w;
scanf("%d%d%lld",&x,&y,&w);
addEdge_SPFA(x,y,w);
}
SPFA(1);
for(int i=1;i<=n;i++){
for(int j=head_SPFA[i];j!=-1;j=edge_SPFA[j].next){
int y=edge_SPFA[j].to;
LL dis=edge_SPFA[j].dis;
if(dis_SPFA[y]==dis_SPFA[i]+dis)
addEdge_Dinic(i,y,dis);
}
}
S=1,T=n;
printf("%lld\n",Dinic());
}
return 0;
}