http://acm.hdu.edu.cn/showproblem.php?pid=6582
Path
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 685 Accepted Submission(s): 189
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 3
Sample Output
3
Source
2019 Multi-University Training Contest 1
给你n个点m个边,问你1-n的最短路为w,删去一些边,使得1-n的最短路长度不为w|或者根本走不通,删去的边的最小长度和是多少
我们先跑最短路得到所有与最短路有关的边,将这些边建图
因为要找最小割边和,也就是最小割,因为最小割就是最大流,所以直接跑最大流即可
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=10005;
const ll INF=1e18;
struct node1
{
int to,cost,next;
}tree[maxn];
int head[maxn];
ll d[maxn];
int cnt;
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
for(int i=0;i<maxn;i++)
d[i]=INF;
}
void add(int u,int v,int w)
{
tree[cnt].to=v;
tree[cnt].cost=w;
tree[cnt].next=head[u];
head[u]=cnt++;
}
struct node2
{
int u,v,w;
}bian[maxn];
void dijstla(int s)
{
typedef pair<ll,int> p;
priority_queue <p,vector<p>,greater<p> > Q;
d[s]=0;
Q.push(p(0,s));
while(!Q.empty())
{
p t=Q.top();
Q.pop();
int x=t.second;
if(d[x]<t.first) continue;
for(int i=head[x];i!=-1;i=tree[i].next)
{
int y=tree[i].to,z=tree[i].cost;
if(d[y]>d[x]+z)
{
d[y]=d[x]+z;
Q.push(p(d[y],y));
}
}
}
}
struct edge
{
int to,cap,rev; //用于表示边的结构体(终点,容量,反向边)
};
vector <edge> G[maxn]; //图的邻接表表示
int level[maxn]; //顶点到源点的距离标号
int iter[maxn]; //当前弧,在其之前的边已经没有用了
void add_edge(int from,int to,int cap)
{
G[from].push_back((edge){to,cap,G[to].size()});
G[to].push_back((edge){from,0,G[from].size()-1});
}
void bfs(int s) //通过bfs计算从源点出发的距离标号
{
memset(level,-1,sizeof(level));
queue <int> q;
level[s]=0;
q.push(s);
while(!q.empty())
{
int v=q.front();q.pop();
for(int i=0;i<G[v].size();i++)
{
edge &e=G[v][i];
if(e.cap>0&&level[e.to]<0)
{
level[e.to]=level[v]+1;
q.push(e.to);
}
}
}
}
int dfs(int v,int t,int f) //通过dfs寻找增广路
{
if(v==t) return f;
for(int &i=iter[v];i<G[v].size();i++)
{
edge &e=G[v][i];
if(e.cap>0&&level[v]<level[e.to])
{
int d=dfs(e.to,t,min(f,e.cap));
if(d>0)
{
e.cap-=d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return 0;
}
ll max_flow(int s,int t)
{
ll flow=0;
for(;;)
{
bfs(s); //计算层次图
if(level[t]<0) return flow; //找不到s-t路径
memset(iter,0,sizeof(iter)); //初始化当前弧
int f;
while((f=dfs(s,t,2e9))>0) //更新最大流
flow+=f;
}
return flow;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
for(int i=0;i<maxn;i++) G[i].clear();
int n,m;
int num=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
if(u==v) continue;
bian[++num].u=u,bian[num].v=v,bian[num].w=w;
add(u,v,w);
}
int s=1,e=n;
dijstla(s);
for(int i=1;i<=num;i++)
{
int u=bian[i].u;
int v=bian[i].v;
int w=bian[i].w;
if(d[u]+w==d[v]) add_edge(u,v,w);
}
printf("%lld\n",max_flow(s,e));
}
return 0;
}