HDU 2485 Destroying the bus stations (IDA * + BFS)

Source: Internet
Author: User
Tags acos

Door: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 2485


Question: Give You n points, m edge, ask you to remove at least a few points to make the minimum path from 1 to n> = k, where 1 and n points cannot be removed.

Problem: this problem can be solved using the smallest stream or IDA * + BFS.


AC code:

# Include <iostream> # include <cstdio> # include <cstring> # include <string> # include <cstdlib> # include <cmath> # include <vector> # include <list> # include <deque> # include <queue> # include <iterator> # include <stack> # include <map> # include <set> # include <algorithm> # include <cctype> using namespace std; # define si1 (a) scanf ("% d", & a) # define I2 (a, B) scanf ("% d", & a, & B) # define sd1 (a) scanf ("% lf", & a) # define sd2 (a, B) Scanf ("% lf", & a, & B) # define ss1 (s) scanf ("% s", s) # define pi1 () printf ("% d \ n", a) # define pi2 (a, B) printf ("% d \ n", a, B) # define mset (, b) memset (a, B, sizeof (a) # define forb (I, a, B) for (int I = a; I <B; I ++) # define ford (I, a, B) for (int I = a; I <= B; I ++) typedef long LL; const int N = 1005; const int M = 105; const int INF = 0x3f3f3f3f; const double PI = acos (-1.0); const double eps = 1e-7; int n, m, k; int lin [N] [N]; // The list of adjacent tables stores the int Fa [N]; // record from 1 to n Shortest Path bool vis [N]; // mark whether the node is deleted int ans, Min; bool flag; struct xh {int x, y, next;} edge [N]; int bfs () {int q [2222]; int he = 0, ta = 0; mset (fa, -1); fa [1] = 0; mset (q, 0); q [ta ++] = 1; while (he! = Ta) {int x = q [he ++]; for (int I = 1; I <= lin [x] [0]; I ++) {int v = lin [x] [I]; if (fa [v] =-1 &&! Vis [v]) {fa [v] = x; q [ta ++] = v; if (n = v) return 1 ;}} return 0 ;} void dfs (int dian, int depth) // remove the minimum number point {if (flag = false) return; if (dian> depth) return; int p = bfs (); int sa [N] = {0}, t = 0; if (p = 0) {flag = false; return ;} for (int I = n; I! = 1; I = fa [I]) {sa [t ++] = I; if (t> k) {flag = false; return ;}} for (int I = 1; I <t; I ++) {int x = sa [I]; if (vis [x]) continue; vis [x] = 1; dfs (dian + 1, depth); vis [x] = 0; if (flag = false) return ;}} void IDA () {if (n <= 2) {puts ("0"); return;} int depth = 0; flag = true; vis [1] = 1; while (flag) {dfs (0, depth ); if (! Flag) break; depth ++;} pi1 (depth);} int main () {// freopen ("input.txt", "r", stdin ); while (scanf ("% d", & n, & m, & k) & (n + m + k) {mset (lin, 0 ); forb (I, 0, m) {int a, B, t; I2 (a, B); t = ++ lin [a] [0]; lin [a] [t] = B; // This place writes two-way WA, one-way A} IDA ();} return 0 ;}




Timeout Code: (IDA * + DFS) I think it should be similar, but it is time-out.

# Include <iostream> # include <cstdio> # include <cstring> # include <string> # include <cstdlib> # include <cmath> # include <vector> # include <list> # include <deque> # include <queue> # include <iterator> # include <stack> # include <map> # include <set> # include <algorithm> # include <cctype> using namespace std; # define si1 (a) scanf ("% d", & a) # define I2 (a, B) scanf ("% d", & a, & B) # define sd1 (a) scanf ("% lf", & a) # define sd2 (a, B) Scanf ("% lf", & a, & B) # define ss1 (s) scanf ("% s", s) # define pi1 () printf ("% d \ n", a) # define pi2 (a, B) printf ("% d \ n", a, B) # define mset (, b) memset (a, B, sizeof (a) # define forb (I, a, B) for (int I = a; I <B; I ++) # define ford (I, a, B) for (int I = a; I <= B; I ++) typedef long LL; const int N = 1005; const int M = 105; const int INF = 0x3f3f3f3f; const double PI = acos (-1.0); const double eps = 1e-7; int n, m, k; int lin [N] [N]; // The list of adjacent tables stores the int Cnt [M]; // record the Shortest Path bool vis [n] from 1 to N; // mark whether the node is deleted int ans, Min; bool flag; struct xh {int x, y, next;} edge [N]; void dfs2 (int k, int de) // calculate the shortest distance {cnt [de] = k; // path if (k = n) {Min = min (Min, de); return;} for (int I = 1; I <= lin [k] [0]; I ++) {int t = lin [k] [I]; if (vis [t]) continue; vis [t] = 1; dfs2 (t, de + 1); vis [t] = 0 ;}} void dfs1 (int dian, int depth) // remove the least number point {if (flag = false) return; Min = INF; mset (cnt, 0); dfs2 (); // cout <"shortest Distance from "<Min <endl; // cout <" Path: "; // if (Min! = INF) // {// for (int I = 0; I <= Min; I ++) // printf ("% d", cnt [I]); // cout <endl; //} // system ("pause"); int xh = Min; if (xh> = k) {flag = false; ans = min (ans, dian); return;} if (dian> = depth) return; int sa [N]; for (int I = 0; I <= xh; I ++) sa [I] = cnt [I]; for (int I = 1; I <xh; I ++) {int t = sa [I]; if (vis [t]) continue; vis [t] = 1; dfs1 (dian + 1, depth); vis [t] = 0; if (flag = false) return ;}} int main () {// freopen ("input.txt", "r", stdin); while (scanf ("% d", & n, & m, & k) & (n + m + k) {mset (lin, 0); forb (I, 0, m) {int a, B, t; i2 (a, B); t = ++ lin [a] [0]; lin [a] [t] = B; t = ++ lin [B] [0]; lin [B] [t] = a;} ans = INF; mset (vis, 0 ); vis [1] = 1; flag = true; int depth =-1; while (flag) {depth ++; dfs1 (0, depth);} pi1 (depth );} return 0 ;}


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.