Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2174 Accepted Submission(s): 668
Problem 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) you can get.
For sample, X 12=X 24=1,all other X ij is 0.
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) you can get.
Hint
For sample, X 12=X 24=1,all other X ij is 0.
Input
The input consists of multiple test cases (less than 35 case).
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 line is C ij(0<=C ij<=100000).
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 line is C ij(0<=C ij<=100000).
Output
For each case, output the minimum of ∑C
ij*X
ij you can get.
Sample Input
4 1 2 4 10 2 0 1 1 2 2 0 5 6 3 1 2
Sample Output
3
Author
Snow_storm
Source
Recommend
zhuyuanchen520
题目大意:
给一个N*N的矩阵,求一个满足给定约束条件的的01N*N矩阵,使得两个矩阵对应位置乘积之和最小。
解题思路:
看到这两个矩阵的形式,就可以联想到邻接矩阵。那么,输入的矩阵就是一个N个顶点的图的描述,要构造的01矩阵就是在这个图上边的选择。
所以我们就可以把题目翻译一下。1号点的出度(不包括自环)为1,N号点的入度(不包括自环)为1,其它顶点入度等于出度,选择一些边满足这些条件,使得这些边的权值之和最小。
满足条件的有两种情况:第一,求从1到N的最短路。第二,求顶点1和顶点N的非自环闭环和的最小值。两种情况取最小值即为答案。
求最短路可以直接用spfa求解。对于非自环最小环,可以手动处理与起点相邻的顶点,然后dist[s]设为inf,跑完spfa后dis[s]即为非自环最小环。
AC代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <stack>
#include <deque>
using namespace std;
#define INF 0x3f3f3f3f
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))
const int MAXV=300+3;
int V,G[MAXV][MAXV];
bool vis[MAXV];
int dist[MAXV];
deque<int> que;
void spfa(int s)
{
for(int u=0;u<V;++u)//手动处理第一步
{
if(u!=s&&G[s][u]!=INF)
{
vis[u]=true;
dist[u]=G[s][u];
if(que.empty()||dist[u]<dist[que.front()])
que.push_front(u);
else que.push_back(u);
}
else
{
vis[u]=false;
dist[u]=INF;
}
}
while(!que.empty())
{
int u=que.front(); que.pop_front();
vis[u]=false;
for(int v=0;v<V;++v)
if(dist[v]>dist[u]+G[u][v])
{
dist[v]=dist[u]+G[u][v];
if(!vis[v])
{
vis[v]=true;
if(que.empty()||dist[v]<dist[que.front()])
que.push_front(v);
else que.push_back(v);
}
}
}
}
int main()
{
while(~scanf("%d",&V))
{
for(int i=0;i<V;++i)
for(int j=0;j<V;++j)
scanf("%d",&G[i][j]);
spfa(0);
int ans=dist[V-1],loop1=dist[0];
spfa(V-1);
int loop2=dist[V-1];
printf("%d\n",min(ans,loop1+loop2));
}
return 0;
}