HDU 3991 Harry Potter and the Present II least path Overwrite

Source: Internet
Author: User

// This question does not occur when it is first encountered .. Then I understood it after reading the report from the experts ..
// N cities, with M routes .. There are Q tasks... at least a few talents are required to complete the task .. Use FLOYD to specify the shortest distance of two points, and then sort them by the time of each task .. Then split the points to build edges ,,
# Include <cstdio>
# Include <iostream>
# Include <algorithm>
Using namespace std;
# Define maxn2001
Constint inf = 1000000001;
Bool mark [maxn];
Int g [maxn] [maxn];
Int map [maxn] [maxn];
Int used [maxn] [maxn];
Int id [maxn] [maxn];
Int m, n, k, x, y;
Int mk [maxn];
// Starting from the vertex u in the X set, use the depth-first policy to find the augmented path.
// (This augmented path can only increase the current number of matches by 1)
Int nx, ny; // Number of vertices in the X and Y Sets
Int cx [maxn], cy [maxn];
// Cx [I] indicates the Y vertex matched with Xi in the final maximum match. The same applies to cy [I ].
Int path (int u)
{
For (int v = 0; v <ny; v ++) // consider all Yi vertices v
{
If (map [u] [v] &! Mk [v])
{
Mk [v] = 1;
// If v does not match, or if v already matches,
// However, starting from y [v], you can find an augmented path.
If (cy [v] =-1 | path (cy [v])
{
Cx [u] = v; // match v to u
Cy [v] = u; // match u to v
Return 1; // find the augmented path
}
}
}
Return 0; // if there is no augmented path from u
}
Int MaxMatch () // calculate the Hungarian algorithm for maximum matching of Two Graphs
{
Int res = 0;
Memset (cx, 0xff, sizeof (cx); // extended from 0 matching
Memset (cy, 0xff, sizeof (cy ));
For (int I = 0; I <= nx; I ++)
{
If (cx [I] =-1) // search for augmented path from each uncovered point
{
Memset (mk, 0, sizeof (mk ));
Res + = path (I); // each time an augmented path is found, the matching number plus 1 is allowed.
}
}
Return res;
}

Typedef struct
{
Int x, y;
} Point;
Point point [1002];
Int cmp (const void * p, const void * q)
{
Point * a, * B;
A = (Point *) p;
B = (Point *) q;
Return a-> y-B-> y;
}
Int main ()
{
Int I, j, k, m, n, q;
Int x, y, z;
Int T = 0, t;
Scanf ("% d", & t );
While (t --)
{
Memset (map, 0, sizeof (map ));
Memset (used, 0, sizeof (used ));
Scanf ("% d", & n, & m, & q );
Nx = ny = q; // both X and Y are Q elements ..
For (I = 0; I <n; I ++) // floyd Initialization
{
For (j = 0; j <n; j ++)
G [I] [j] = inf;
G [I] [I] = 0;
}
For (I = 0; I <m; I ++)
{
Scanf ("% d", & x, & y, & z );
If (g [x] [y]> z)
G [x] [y] = g [y] [x] = z;
}
For (k = 0; k <n; k ++)
{
For (I = 0; I <n; I ++)
{
For (j = 0; j <n; j ++)
{
If (g [I] [j]> g [I] [k] + g [k] [j])
G [I] [j] = g [I] [k] + g [k] [j];
}
}
}
For (I = 0; I <q; I ++)
{
Scanf ("% d", & point [I]. x, & point [I]. y );
}
Qsort (point, q, sizeof (point [0]), cmp );
For (I = 0; I <q; I ++) // minimum path overwrite, split a vertex into an inbound and outbound vertex. Create an edge between an outbound vertex and an inbound vertex.
{
For (j = I + 1; j <q; j ++)
{
If (point [j]. y-point [I]. y> = g [point [I]. x] [point [j]. x]) // match the conditions ..
{
Map [I] [j] = 1;
}
}
}
Printf ("Case % d:", ++ T );
Printf ("% d \ n", q-MaxMatch ()-1); // subtract Harry Potter
}
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.