Test instructions: Given n points, the E-bar and the length of each edge t, each of the two points are connected, allowing you to find a shortest path through the e-side.
Analysis: Just began to think of the connection, and then the corresponding several pieces add up, but, the second example is but, then a think, then there is a Euro-loop has to add 1 ah.
Also want to judge each time is not the euro-pull circuit, how to judge is the problem, because do not know which is connected in a piece, you have to find again, trouble ah ....
Later on the internet to look at the puzzle, the original is to construct Ah, that is, each connected block is structured into a European pull circuit, then minus the end point, it can be fully connected.
It's a good way, the Euler loop satisfies the degree of each point is even, that is, if it is not an even number, then we have to add a man, and finally calculate how many we added,
Then add the original title of the article E, is the last number.
The code is as follows:
#include <iostream> #include <cstdio> #include <vector> #include <cstring>using namespace std; const int MAXN = 10;int T, N, e;vector<int> g[maxn];bool vis[maxn];int dfs (int u) {if (Vis[u]) return 0; Vis[u] = true; int ans = (g[u].size () & 1); for (int i = 0; i < g[u].size (); ++i) ans + = DFS (g[u][i]); return ans;} int solve () {int ans = 0; for (int i = 1; I <= n; ++i) if (! G[i].empty () &&!vis[i]) ans + = max (Dfs (i), 2); Return Max ((ans-2)/2, 0) + e;} int main () {//Freopen ("In.txt", "R", stdin); int kase = 0; while (scanf ("%d%d", &n, &e, &t) = = 3) {if (!n &&!e &&!t) break; int u, v; for (int i = 1; I <= n; ++i) g[i].clear (); memset (Vis, 0, sizeof (VIS)); for (int i = 0; i < e; ++i) {scanf ("%d%d", &u, &v); G[u].push_back (v); G[v].push_back (U); } printf ("Case %d:%d\n ", ++kase, T * solve ()); } return 0;}
UVa 12118 Nspector ' s dilemma (tectonic +dfs+ Euler circuit)