HDU5294 Tricks Device for Problem solving report

Source: Internet
Author: User

HDU5294 Tricks Device for Problem solving report

Description

Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu's at the entrance of the tomb while Dumb Zhang's at the end of it. The tomb is made up of many chambers, and the total number is N. And there is M channels connecting the chambers. Innocent Wu wants to catch up Dumb-to-find out the answers of some questions, however, it ' s Dumb Zhang ' s intention t o Keep Innocent Wu in the dark, to do which he have to stop Innocent Wu from getting him. Only via the original shortest ways from the entrance to the end of the tomb costs the minimum time, and that's the only C Hance Innocent Wu can catch Dumb Zhang.
Unfortunately, Dumb Zhang masters The Art of becoming invisible (qi men dun jia) and tricks devices of this tomb, he can cut off the Connections between chambers by using them. Dumb Zhang wanders how many channels at least he had to cut to stop Innocent Wu. And Innocent Wu wants to know after what many channels at most Dumb Zhang cut off Innocent Wu still have the chance to catch Dumb Zhang.

Input

There is multiple test cases. Please process till EOF.
For each case,the first line must includes the integers, N (<=2000), M (<=60000). N is the total number of the chambers, and M is the total number of the channels.
In the following M lines, every line must includes three numbers, and use AI, Bi, Li as channel i connecting Chamber AI and Bi (1<=ai,bi<=n), it costs Li (0<li<=100) minute to pass channel I.
The entrance of the tomb is at the chamber one, and the end of tomb is at the chamber N.

Output

Output of numbers to stand for the answers of Dumb Zhang and Innocent Wu ' s questions.

Sample Input

Sample Output


The main idea is to give a graph with n nodes without direction, starting at 1 and ending with N. For all of the 1 to N of the most short-circuited set of sub-diagram, the first question is to delete at least a few sides to make the remaining figure 1 is not to N, the second question is that in this sub-graph to delete a few edges can also make 1 can reach N.
Analysis: First of all inevitably face a problem, is how to determine whether an edge is not the shortest edge. The first idea is to run the shortest circuit at once, then enumerate each edge (u,v), if 1 to u minimum path +uv length = 1 to v the shortest path, then it is (U,V) is the quickest way. Unfortunately this idea is wrong, this can only prove that it is the shortest from U to V, but not necessarily the shortest 1 to n (see picture you understand).



For example, the above figure, if the previous method to make, then the above four edges will be added, the following two edges will be added. But it is wrong, because the road above is not the shortest way at all. So we need to change the method, can guarantee that a road is 1--N of the shortest part of the method is to run the shortest time, respectively, 1 and N for the source, for an edge (U,V), if (1->u) min + UV + (v->n) min = = (1->n) min is the shortest way (of course, it is also equal to (n->1) min ).
Well, the first problem is solved. We first run the shortest two, and then sift the edges of the quickest path to create a new sub-map. Then ask first, to delete a few sides, then a look is the smallest cut, the smallest cut is also the maximum flow. Then set each side of the sub-graph flow to 1, and then run the maximum flow is the answer.
So how do you get the second question? The idea is to find a minimum number of edges of the shortest, and then the total number of edges M-this road number is the answer. Then we can run the shortest path on the sub-graph, because the previous sub-graph traffic (also as a weight) has been set to 1. So at this point in the 1 as the source to run on the sub-chart the shortest path is the least of all the short-circuit 1->n from the minimum number of edges. (note here, the so-called shortest path in the number of sides is the least, we have used all the shortest possible edges structure This sub-chart, has guaranteed the smallest weight, and then see the least number of sides)
on the code:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < queue> #include <deque> #include <vector> #include <utility>using namespace std;const int maxn = 2510 ; const int MAXM = 300010;const int INF = 0x3f3f3f3f;struct edge{int from, to, cap, next;}; Edge EDGE[MAXM]; Edge edge2[maxm];int head[maxn];int head2[maxn];int level[maxn];int dis1[maxn], dis2[maxn];int src, des, cnt, cnt2;void S PFA (int *dis, int src,int n) {int INQUEUE[MAXN] = {0};d eque<int> q;dis[src] = 0;inqueue[src] = 1;q.push_front (SRC); while (!q.empty ()) {int u = q.front (), Q.pop_front (), for (int i = head2[u]; I!=-1;i=edge2[i].next) {int v = edge2[i].to;if (di S[V]&GT;DIS[U]+EDGE2[I].CAP) {Dis[v] = Dis[u] + edge2[i].cap;if (!inqueue[v]) {if (!q.empty () && dis[v] > dis[ Q.front ()]) Q.push_front (v); Elseq.push_back (v);}}} void Addedge (int from, int to, int cap) {Edge[cnt].from = From;edge[cnt].to = To;edge[cnt].cap = Cap;edge[cnt].next = Hea d[from];head[From] = Cnt++;swap (from, to); Edge[cnt].from = from;edge[cnt].to = To;edge[cnt].cap = 0;edge[cnt].next = Head[from];head [From] = cnt++;} void Addedge2 (int from, int to, int cap) {Edge2[cnt2].from = From;edge2[cnt2].to = To;edge2[cnt2].cap = CAP;EDGE2[CNT2].N ext = Head2[from];head2[from] = cnt2++;} BOOL BFs () {memset (level,-1, sizeof level);queue<int> Q;while (!q.empty ()) Q.pop (); LEVEL[SRC] = 0;q.push (src); whi  Le (!q.empty ()) {int u = q.front (); Q.pop (); for (int i = head[u]; I! = 1; i = edge[i].next) {int v = edge[i].to;if (edge[i].cap > 0 && level[v] = = 1) {Level[v] = Level[u] + 1;q.push (v); if (v = = des) return level[des]! =-1;}} return Level[des]! =-1;} int dfs (int u, int f) {if (U = = des) return f;int tem;for (int i = head[u]; I! =-1; i = edge[i].next) {int v = edge[i].to;i  F (Edge[i].cap > 0 && level[v] = = Level[u] + 1) {tem = DFS (V, min (f, edge[i].cap)); if (Tem > 0) {edge[i].cap -= tem;edge[i ^ 1].cap + = Tem;return tem;}} Level[u] = -1;return 0;} int DiniC () {int ans = 0, tem = 0;while (BFS ()) {tem = DFS (SRC, INF), if (tem > 0) ans + = tem;} return ans;} int main () {int n, m;while (CIN >> n >> m) {cnt = Cnt2 = 0;SRC = 1; des = N;memset (head,-1, sizeof head); Memse T (head2,-1, sizeof head2), memset (DIS1, INF, sizeof dis1), memset (DIS2, INF, sizeof DIS2), int A, B, c;for (int i = 1; I <= m; i++) {scanf ("%d%d%d", &a,&b,&c); Addedge2 (A, B, c); Addedge2 (b, A, c);} SPFA (DIS1, 1, N); SPFA (Dis2, n, N); for (int i = 1; I <= n; i++)//re-composition {for (int j = head2[i]; J! =-1; j = edge2[j].next) {int v = edge2[j] . to;if (Dis1[i] + edge2[j].cap + dis2[v] = = Dis1[n]) {Addedge (i, V, 1); edge2[j].cap = 1;} Else{edge2[j].cap = INF;}}} memset (Dis1, INF, sizeof dis1); SPFA (DIS1, 1, N), cout << dinic () << "<< M-dis1[n]<<endl;} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU5294 Tricks Device for Problem solving report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.