Medium HDU 4370 0 or 1, modelling +dijkstra.

Source: Internet
Author: User

Description

Given a n*n matrix C ij (1<=i,j<=n), We want to find a n*n matrix X ij (1<=i,j<=n), which is 0 or 1.

Besides,x IJ meets the following conditions:

1.X 12+x 13+ ... X 1n=1
2.X 1n+x 2n+ ... X N-1n=1
3.for each I (1<i<n), Satisfies∑x ki (1<=k<=n) =∑x ij (1<=j<=n).

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

X 12+x 13+x 14=1
X 14+x 24+x 34=1
X 12+x 22+x 32+x 42=x 21+x 22+x 23+x 24
X 13+x 23+x 33+x 43=x 31+x 32+x 33+x 34

Now, we want to know the minimum of∑c ij*x ij (1<=i,j<=n) can get.

The problem can be converted to the shortest path. The problem can be 1.1 points of analysis, the first is to select the first row of the K after the choice of a K line, so the edge of the 1->k edge of the first row of the value of K, and then the shortest way to find 1->n is good ...

There is, however, a special case where 1 and the other form a ring, N and the other form a ring. So the answer is two rings of the and, and the shortest short of the small one ...

The code is as follows:

#include <stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<time.h>using namespacestd;Const intmaxn= -;Const intinf=10e8;voidDijkstra (intCOST[][MAXN],intLowcost[],intNintstart) {Priority_queue<int>que; intT;  for(intI=1; i<=n;++i) lowcost[i]=INF;    Que.push (start);  while(!Que.empty ()) {T=Que.top ();        Que.pop ();  for(intI=1; i<=n;++i)if(i!=t)if(Lowcost[t]==inf | | lowcost[i]>lowcost[t]+Cost[t][i]) {Lowcost[i]= (Lowcost[t]==inf?)0: lowcost[t]) +Cost[t][i];                Que.push (i); }    }}intMAP1[MAXN][MAXN];intANS[MAXN];intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);    intN; intt1,t2;  while(~SCANF ("%d",&N)) { for(intI=1; i<=n;++i) for(intj=1; j<=n;++j) scanf ("%d",&Map1[i][j]); Dijkstra (Map1,ans,n,1); T1=Ans[n]; T2=ans[1];        Dijkstra (Map1,ans,n,n); T2+=Ans[n]; printf ("%d\n", Min (t1,t2)); }        return 0;}
View Code

Medium HDU 4370 0 or 1, modelling +dijkstra.

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.