Topic: 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): 3747 Accepted Submission(s): 1075
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.
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.
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
Recommend
Meaning of the questions:
To n nodes and m have the edge, the edge is now possible to delete some of the consideration of the right side asked to delete some of the least-cost edge makes now the most short-circuit node 1 to node n does not hold (1 if the answer is not up to n 0 )
Ideas:
There may be multiple shortest 1 to n, the shortest composition diagram, FIG minimum cost so that no communication request is minimum cut, the theorem, the maximum flow, maximum flow equal to the minimum cut, we first find the figures 1 to n of the shortest path to create a new map on this figure from 1 to n running while the maximum flow can find the answer to
the problem now is to build graphs, let's get the shortest path from 1 to n dis [i], then get the shortest path to dis1 n 1 [i], then the original line with dis [u] + e [i ] .w + dis1 [v] == dis [n] ( order e [i] .w node u to the right side of v) side to build a new map
on this new plan can get an answer from 1 to n running while dinic
note:
Adjusting an afternoon, in bool operator (const node & a) const {return aw <w;} originally to aw <w to make priority queue w smaller priority, if it is aw> w is w [priority
1 #include<bits/stdc++.h> 2 typedef long long ll; 3 using namespace std; 4 const int amn=1e4+5; 5 const ll inf=1e18; 6 struct edge{ 7 int from,to,nex;ll w; 8 }eg[amn],eg1[amn],e[amn<<2|1]; 9 struct node{ 10 int p; 11 ll w; 12 node(intPP, LL WW) PP = {P; W = WW;} 13 is BOOL operator <( const Node A &) const { return aw <w;} /// tune an afternoon turned to AW <W job, so priority queue priority only small w, if AW> w w is the major priority 14 }; 15 LL head [AMN], EGN, head1 [AMN], EGN1, n-, m, DIS [AMN], DIS1 [AMN], VIS [AMN], head2 the [AMN], egn2; 16 void the init ( int n-) { . 17 EGN = 0 , EGN1 = 0 , egn2 = 1 ; /// egn2 is running maximum flow, initially 1 so that it can start from the plus side 2, so that the forward side and reverse side adjacent to the storage, because 2 = 1 ^ 3,3 ^ 1 = 2 ... can be obtained exclusive or reverse side forward edge 18 is for (int i=1;i<=n;i++)head2[i]=head1[i]=head[i]=0; 19 } 20 void add(int u,int v,ll w){ ///正向图加边 21 eg[++egn].nex=head[u]; 22 head[u]=egn; 23 eg[egn].from=u; 24 eg[egn].to=v; 25 eg[egn].w=w; 26 } 27 void add1(int u,int v,ll w){ ///反向图加边 28 EG1 [EGN1 ++] = .nex head1 [U]; 29 head1 [U] = EGN1; 30 . EG1 [EGN1] from = U; 31 is EG1 [EGN1] .to = V; 32 EG1 [EGN1] .W = W; 33 is } 34 is void ADD2 ( int U, int V, W LL) { /// run new maximum flow edging FIG 35 E [++ egn2] .nex = head2 the [U]; 36 head2 the [U] = egn2; 37 [ E [egn2]. from = U; 38 is E [egn2] .to =V; 39 E [egn2] .W = W; 40 } 41 is 42 is /// to 1 starting from the shortest run 43 is void Dijkstra ( int S) { 44 is Memset (VIS, 0 , the sizeof VIS); 45 for ( int I = 0 ; <= n-I; I ++) DIS [I] = INF; 46 is DIS [S] = 0 ; 47 The priority_queue <Node> Q; 48 q.push (Node (S, DIS [S])); 49 the while (q.size ()) { 50 int U = q.top () P.; 51 ll w=q.top().w;q.pop(); 52 if(w>dis[u])continue; 53 vis[u]=1; 54 for(int i=head[u];i;i=eg[i].nex){ 55 int v=eg[i].to; 56 if(!vis[v]&&((dis[v])>(eg[i].w)+(dis[u]))){ 57 dis[v]=(eg[i].w)+dis[u]; 58 q.push(node(v,(dis[v]))); 59 } 60 } 61 } 62 } 63 ///以n为起点跑最短路 64 void dijkstra1(int s){ 65 memset(vis,0,sizeof vis); 66 for(int i=0;i<=n;i++)dis1[i]=inf; 67 dis1[s]=0; 68 priority_queue<node> q; 69 q.push(node(s,dis1[s])); 70 while(q.size()){ 71 int u=q.top().p; 72 ll w=q.top().w;q.pop(); 73 if(w>dis1[u])continue; 74 vis[u]=1; 75 for(int i=head1[u];i;i=eg1[i].nex){ 76 int v=eg1[i].to; 77 if(!vis[v]&&((dis1[v])>(eg1[i].w)+(dis1[u]))){ 78 dis1[v]=(eg1[i].w)+dis1[u]; 79 q.push(node(v,(dis1[v]))); 80 } 81 } 82 } 83 } 84 ///dinic最大流 85 queue<int> que; 86 ll dist[amn],src=1,sink=n; 87 void bfs(){ 88 memset(dist,0,sizeof dist); 89 while(que.size())que.pop(); 90 vis[src]=1; 91 que.push(src); 92 while(que.size()){ 93 int u=que.front();que.pop(); 94 for(int i=head2[u];i;i=e[i].nex){ 95 int v=e[i].to;//cout<<'>'<<e[i].w<<' '<<v<<endl; 96 if(e[i].w&&!vis[v]){ 97 que.push(v); 98 dist[v]=dist[u]+1; 99 vis[v]=1; 100 } 101 } 102 } 103 } 104 int dfs(int u,ll delta){ 105 if(u==sink)return delta; 106 int ret=0; 107 for(int i=head2[u];delta&&i;i=e[i].nex) 108 if(e[i].w&&(dist[e[i].to]==dist[u]+1)){ 109 ll dd=dfs(e[i].to,min(e[i].w,delta)); 110 e[i].w-=dd; 111 e[i^1].w+=dd; 112 delta-=dd; 113 ret+=dd; 114 } 115 return ret; 116 } 117 ll maxflow(){ 118 ll ret=0; 119 while(1){ 120 memset(vis,0,sizeof vis); 121 bfs(); 122 if(!vis[sink])return ret;//cout<<'<'<<ret<<endl; 123 ret+=dfs(src,inf); 124 } 125 } 126 int main(){ 127 int T,x,y;ll c; 128 scanf("%d",&T); 129 while(T--){ 130 scanf("%lld%lld",&n,&m); 131 init(n); ///初始化 132 the src = 1 , = n-sink; /// set a point source, n is the sink node 133 for ( int I = 1 ; I <= m; I ++ ) { 134 Scanf ( " % D% D% LLD " , X &, & Y, & C); 135 the Add (X, Y, C); /// forward view in order starting from a shortest run 136 ADD1 (Y, X, C); /// reverse FIG. for n starting at the shortest run 137 } 138 Dijkstra ( 1 ); /// to a shortest path starting from the 139 IF (DIS [n] == INF) {the printf ( " 0 \ n");continue;} ///如果1到n不可达则输出0 140 dijkstra1(n); ///以n为起点的最短路 141 for(int i=1;i<=egn;i++){ 142 // cout<<eg[i].from<<' '<<eg[i].to<<' '<<eg[i].w<<endl; 143 // cout<<dis[eg[i].from]<<' '<<eg[i].w<<' '<<dis1[eg[i].to]<<' '<<dis[n]<<endl<<endl; 144 if(dis[eg[i].from]+eg[i].w+dis1[eg[i].to] == DIS [n]) { ADD2 (EG [I].145If now the point u to a shortest path to the edge weights v + u + v is the shortest path n 1 to n == shortest path from u to v is this the edge is an edge in the shortest/// from , EG [I] .to, EG [I] .W); /// build a new view is forward edge 146 ADD2 (EG [I] .to, EG [I]. from , 0 ); /// 0 reverse side the right side 147 } 148 } 149 the printf ( " % LLD \ n " , MaxFlow ()); /// maximum flow equal to the minimum cut 150 } 151 } 152 / * * 153 to the n nodes and m directed edges, can now be deleted some of the side, the right side is the price, ask the minimum cost by deleting some of the shortest side now makes the node 1 to node n does not hold (1 if the answer is not up to n 0) 154 may be multiple 1 to n shortest, most of these short circuit composition diagram, FIG minimum cost so that no communication request is minimum cut, the theorem, the maximum flow, maximum flow equal to the minimum cut, we first find the figures 1 to n shortest a new view in this figure running from 1 to n can be obtained while the maximum flow answers 155 FIG problem now is built, we first obtain the shortest path n 1 to dis [i], then give the shortest path dis1 [i] n to 1, then the original line with dis [u] + e [i ]. w + dis1 [v] == dis [n] ( order e [i] .w u to node v of the right side) to build a new side view 156 in FIG new running from 1 to n on the side dinic obtained answer 157 tune a PM, bool operator (const node & a) const {return aw <w;} originally to aw <w to make priority queue small w priority, if it is aw> w it is w big priority 158 * * /