A New Graph GameTime
limit:8000/4000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1849 Accepted Submission (s): 802
Problem Descriptionan undirected graph is a graph in which the nodes was connected by undirected arcs. An undirected arc is a edge that has no arrow. Both ends of an undirected arc be equivalent--there is no head or tail. Therefore, we represent an edge on an undirected graph as a set rather than a ordered pair.
Now given a undirected graph, you could delete any number of edges as you wish. Then you'll get one or more connected sub graph from the original one (any of them should has more than one vertex).
You goal are to do all the connected sub graphs exist the Hamiltonian circuit after the delete operation. What's more, you want to know the minimum sum of the weight of the edges in the "Hamiltonian circuit" of all the Conne CTED Sub Graphs (only one "Hamiltonian circuit" would be calculated in one connected sub graph! That's to say if there exist more than one "Hamiltonian circuit" in one connected sub graph, you could only choose the on E in which the sum of weight of these edges is minimum).
For example, we may get the possible sums:
(1) 7 + 10 + 5 = 22
(2) 7 + 10 + 2 = 19
(There is "Hamiltonian circuit" in this graph!)
Inputin The first line there was an integer T, indicates the number of test cases. (T <= 20)
In each case, the first line contains integers n and m, indicates the number of vertices and the number of edges. (1 <= N <=1000, 0 <= m <= 10000)
Then M. Lines, each line contains three integers a,b,c, indicates that there is one edge between A and B, and the weight of It is c. (1 <= A, <= N, a is not equal to B on any, 1 <= C <= 10000)
Outputoutput "Case%d:" First where D was the case number counted from one. Then output ' No ' if there is NO-to-get some connected sub graphs that any of them exists the Hamiltonian circuit after The delete operation. Otherwise, output the minimum sum of weight you may get if you delete the edges in the optimal strategy.
Sample Input
33 41 2 52 1 22 3 103 1 7 3 21 2 31 2 42 21 2 31 2 4
Sample Output
Case 1:19case 2:nocase 3:6Hintin case 1:you could delete edge between 1 and 2 whose weight is 5. In case 2:it's impossible to get some connected sub graphs this any of them exists the Hamiltonian circuit after the Delet E operation.
Authoraekdycoin
Source2010 acm-icpc multi-university Training Contest (1)--host by fzu
#include <stdio.h> #include <string.h> #include <queue>using namespace std;const int maxn = 10010;const int MAXM = 100100;const int INF = 1<<30;struct edg{int to,next,cap,flow; int cost; Unit price per side}edg[maxm];int Head[maxn],eid;int PRE[MAXN], COST[MAXN]; Point 0~ (n-1) void init () {eid=0; memset (head,-1,sizeof (Head));} void addedg (int u,int v,int cap,int cst) {edg[eid].to=v; edg[eid].next=head[u]; edg[eid].cost = CST; Edg[eid].cap=cap; edg[eid].flow=0; head[u]=eid++; Edg[eid].to=u; EDG[EID].NEXT=HEAD[V]; Edg[eid].cost =-CST; Edg[eid].cap=0; edg[eid].flow=0; head[v]=eid++;} BOOL Inq[maxn];bool SPFA (int snode,int enode,int n) {queue<int>q; for (int i=0; i<n; i++) {inq[i]=false; cost[i]= INF; } cost[snode]=0; Inq[snode]=1; Pre[snode]=-1; Q.push (SNode); while (!q.empty ()) {int U=q.front (); Q.pop (); inq[u]=0; for (int i=head[u]; i!=-1; i=edg[i].next) {int v=edg[i].to; if (edg[i].caP-edg[i].flow>0 && cost[v]>cost[u]+edg[i].cost) {//In the case of an incremental flow, the minimum cost cost[v] = cost[u]+edg[i].c Ost Pre[v]=i; Record the edge on the path if (!inq[v]) Q.push (v), inq[v]=1; }}} return cost[enode]!=inf; Determine if there is no augmented path}//reverse is the maximum flow, the minimum cost is mincostint mincost_maxflow (int snode,int enode,int& mincost,int n) {int ans=0; while (SPFA (snode,enode,n)) {ans++; for (int i=pre[enode]; i!=-1; i=pre[edg[i^1].to]) {edg[i].flow+=1; edg[i^1].flow-=1; Mincost+=edg[i].cost; }} return ans; void scanf (int &ans) {char ch; while (Ch=getchar ()) {if (ch>= ' 0 ' &&ch<= ' 9 ') break; } ans=ch-' 0 '; while (Ch=getchar ()) {if (ch< ' 0 ' | | Ch> ' 9 ') break; ans=ans*10+ch-' 0 '; }}int Mapt[1005][1005];int Main () {int t,_case=0,n,m, u, V, D; scanf (T); while (t--) {scanf (n); scanf (m); Init (); int s=0, t=2*n+1; for (int i=1; i<=n; i++) {ADDEDG (S, I, 1, 0); ADDEDG (I+n, T, 1, 0); for (int j=1; j<=n; j + +) Mapt[i][j]=inf; } while (m--) {scanf (U); scanf (v); scanf (d); if (mapt[u][v]>d) Mapt[u][v]=mapt[v][u]=d; } for (u=1; u<=n; u++) for (v=1; v<=n; v++) if (mapt[u][v]!=inf) addedg (U, V+N,1,MAPT[U][V]); int mincost=0; n-= Mincost_maxflow (S, T, Mincost, t+1); printf ("Case%d:", ++_case); if (n==0) printf ("%d\n", mincost); else printf ("no\n"); }}
HDU 3435 A New Graph Game (minimum cost maximum flow) &hdu 3488