HDU 4370 0 or 1 (shortest circuit + minimum ring judgment)

Source: Internet
Author: User

0 or 1

Time limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1421 Accepted Submission (s): 388


Problem Descriptiongiven a n*n matrix Cij (1<=i,j<=n), We want to find a n*n matrix Xij (1<=i,j<=n), which is 0 or 1.

Besides,xij meets the following conditions:

1.x12+x13+ ... X1n=1
2.x1n+x2n+ ... Xn-1n=1
3.for each I (1<i<n), Satisfies∑xki (1<=k<=n) =∑xij (1<=j<=n).

For example, if N=4,we can get the following equality:

X12+x13+x14=1
X14+x24+x34=1
x12+x22+x32+x42=x21+x22+x23+x24
X13+x23+x33+x43=x31+x32+x33+x34

Now, we want to know the minimum Of∑cij*xij (1<=i,j<=n) you can get.
Hint
For sample, X12=x24=1,all other Xij is 0.

Inputthe input consists of multiple test cases (less than).
For each test case, the first line contains one integer n (1<n<=300).
The next n lines, for each lines, each of which contains n integers, illustrating the matrix C, the j-th integer on i-th l INE is Cij (0<=cij<=100000).

Outputfor each case, output of the minimum of∑cij*xij you can get.

Sample Input41 2 4 102 0 1 12 2 0 56 3 1 2

Sample OUTPUT3

Authorsnow_storm

Source2012 multi-university Training Contest 8

Test instructions: Given a matrix Aij, to find a 0-1 matrix bij makes the value of ∑aij*bij minimal.

Requirements for the 0-1 matrix:

1.x12+x13+ ... X1n=1
2.x1n+x2n+ ... Xn-1n=1
3.for each I (1<i<n), Satisfies∑xki (1<=k<=n) =∑xij (1<=j<=n).

Analysis: Sticking to the official puzzle

1001 (Updated)

Obviously, the topic is a 0/1 planning model.

The key to solving the problem is how to see the nature of the model.

3 conditions obviously in describing the relationship between unknowns, from the perspective of graph theory, it is easy to get the following 3 conclusions:

1.x12+x13+ ... X1n=1 so the number 1th node is 1.

2..x1n+x2n+ ... Xn-1n=1 the number of n nodes is 1

3.∑xki =∑xij so that the 2~n-1 node must be equal to the degree of the degree

So the 3 conditions are equivalent to a path from node 1th to N, so xij=1 indicates the need to pass through the Edge (I,J), at the cost of CIJ. Xij=0 indicates that the edge is not passed (i,j). Note that CIJ is not negative and the total cost of the problem is minimal, so the path of the optimal answer must correspond to a simple path.

Finally, we read directly into the edge of the adjacency matrix, run a 1 to n the shortest possible, remember the shortest path.

The above situation is set to a

Very, very, very, very, very, very, very, very, very sorry, simple path is only sufficient condition, but not necessary. (Sorry for the troubled team)

The following case is missing B:

Starting from 1, take a ring (at least 1 points, which cannot be self-loops), go back to 1, start from N, take a ring (same way), and return to N.

Easy to verify, this is in line with the conditions of the problem. and a | | B is the necessary and sufficient condition for the question requirement.

Since the edge is not negative, two rings correspond to two simple rings.

So we can start from 1, find a minimum cost ring, remember the cost of C1, and then from N, find a minimum cost ring, remember the cost of C2. (simply add a record when the shortest path algorithm updates the weights: if (i==s) cir=min (Cir,dis[u]+g[u][i]))

So the final answer is min (path,c1+c2)

#include <cstdio>#include<string>#include<iostream>#include<cstring>#include<cmath>#include<stack>#include<queue>#include<vector>#include<map>#include<stdlib.h>#include<algorithm>#defineLL __int64using namespacestd;Const intmaxn= -+5;Const intinf=0x3f3f3f3f;intW[MAXN][MAXN];intD[MAXN];intINQ[MAXN];intN;queue<int>Q;intSPFA (intStinten) {    intorz=1;  for(intI=1; i<=n;i++) D[i]=INF; D[ST]=0;    Q.push (ST);  while(!Q.empty ()) {        intu=Q.front ();        Q.pop (); Inq[u]=0;  for(intI=1; i<=n;i++)        {            if(d[u]+w[u][i]<D[i]) {D[i]=d[u]+W[u][i]; if(!inq[i]) {inq[i]=1;            Q.push (i);} }        }        if(ORZ) d[st]=inf,orz=0; }    returnD[en];}intMain () {//freopen ("In.txt", "R", stdin);     while(SCANF ("%d", &n)! =EOF) {memset (W,0,sizeof(w));  for(intI=1; i<=n;i++)             for(intj=1; j<=n;j++) scanf ("%d",&W[i][j]); intANS=SPFA (1, N); intRES1=SPFA (1,1); intRes2=SPFA (n,n); printf ("%d\n", Min (ans,res1+res2)); }    return 0;}
View Code

HDU 4370 0 or 1 (shortest circuit + minimum ring judgment)

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.