Poj 3921/hdu 2485 Destroying the bus stations)

Source: Internet
Author: User

A directed graph is given. Each side has a length of 1. delete at least a few points, make the shortest path length from 1 to n greater than k (number of non-intersecting paths whose length is less than or equal to k)

The first method is AC:

First, find the shortest path. For the vertices of d [1] [I] + d [I] [n]> k, it is not necessary to remove them. Then, for the remaining vertices, calculate how many points are deleted to make them not connected. For a typical directed graph point connectivity problem, obtain the minimum cut for the split point, but cannot pass this set of data:

8 10 51 22 33 44 55 66 81 77 84 77 4

Draw a picture to clearly know where the error is, and then there will be a second practice that can be AC:

Split each vertex into two vertices, a1 and a2, and connect an edge with a capacity of 1 and a cost of 0. For each data entry, directed edge a --> B, connect to an a2 --> b1 with a capacity of INF and a fee of 1

The rest is the cost flow. When the shortest path is greater than K, the number of non-intersecting paths cannot be obtained. For example, the following data:

10 11 51 2 2 33 44 55 102 91 66 77 88 99 10
So there is a third approach, the simplest approach, but the complexity is not well estimated, it should be clearly correct:
Dfs search, find the shortest path at each layer, enumerate and delete the vertices in the shortest path, and enter the next layer until the shortest path is greater than k. Update the answer (bfs is incorrect, because bfs is equivalent to the billing method)
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.StreamTokenizer;import java.util.Arrays;public class Main {class SPFA {int maxn = 110, maxm = 5010;int inf = 1 << 30;class node {int be, ne;int val;node(int b, int e, int v) {be = b;ne = e;val = v;}}node buf[] = new node[maxm];int len, E[] = new int[maxn], n, queue[] = new int[maxn];int d[] = new int[maxn];void init(int n) {this.n = n;Arrays.fill(E, -1);len = 0;}void add(int a, int b, int v) {if (a == b)return;buf[len] = new node(b, E[a], v);E[a] = len++;}int pre[] = new int[maxn];boolean vis[] = new boolean[maxn];int solve(int s, int t) {int head = 0, tail = 0;Arrays.fill(vis, false);Arrays.fill(d, inf);Arrays.fill(pre, -1);d[s] = 0;queue[(tail++) % maxn] = s;vis[s] = true;int a, b;while (head != tail) {a = queue[(head++) % maxn];vis[a] = false;for (int i = E[a]; i != -1; i = buf[i].ne) {b = buf[i].be;if (d[a] + buf[i].val < d[b]) {d[b] = d[a] + buf[i].val;pre[b] = a;if (!vis[b]) {vis[b] = true;queue[(tail++) % maxn] = b;}}}}return d[t];}}SPFA sp = new SPFA();int ans, n, m, k;void dfs(int cnt) {int temp = sp.solve(1, n);if (temp > k) {ans = Math.min(ans, cnt);return;}if(cnt+1>=ans)return;int stack[]=new int[n+1],top=0;int v=sp.pre[n];while(v!=1){stack[top++]=v;v=sp.pre[v];}for(int i=0;i<top;i++){temp=sp.E[stack[i]];sp.E[stack[i]]=-1;dfs(cnt+1);sp.E[stack[i]]=temp;}}void run() throws IOException {while (true) {n = nextInt();m = nextInt();k = nextInt();if (n == 0)break;sp.init(n);while (m-- > 0)sp.add(nextInt(), nextInt(), 1);ans=n;dfs(0);System.out.println(ans);}}StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));int nextInt() throws IOException {in.nextToken();return (int) in.nval;}public static void main(String[] args) throws IOException {new Main().run();}}

Ps: in the field competition, there were such irresponsible questions .... Really chilling

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.