hdu 4370 0 or 1 (最短路)

hdu 4370 0 or 1

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.

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).

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

Hint

For sample, X 12=X 24=1,all other X ij is 0.

题目大意:有一个矩阵C[i][j],和一个由01组成的矩阵X[i][j]。X矩阵满足条件:

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).

现在要求最小的∑C ij*X ij。

解题思路:思维转换过来就好做了。从第一个条件可以看出一号结点出度为1,从第二个条件可以看出n号节点的入度为1,从第三个条件可以看出2~n-1号节点的入度必须等于出度。所以可以直接把C[i][j]看成是一张邻接矩阵,1为起点,n为终点,跑一次最短路,求出最短路sp。

这是其中一种情况,还有另一种情况满足题目条件,就是,1号结点有非自环的闭环,n号结点也有非自环的闭环。以1为起点,但d[1]置为INF,且起点不入队列,而让,与1号结点连向的点进入队列,然后跑最短路,最后d[1]就是1结点的最小闭环b1,n的最小闭环b2同理。

所以最后答案就是min(sp, b1 + b2)。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std;

const int N = 305;
const int M = 90000;
const int INF = 0x3f3f3f3f;
typedef long long ll;
int n, Gra[N][N];
int d[N], vis[N], en;
int head[M];

struct node {
    int to, dis, next;
}edge[M];  

void addEdge(int u,int v,int x) {
    edge[en].to = v;
    edge[en].next = head[u];
    edge[en].dis = x;
    head[u] = en++;
}  

void SPFA(int s) {
    queue<int> Q;
    for (int i = 0; i <= n; i++) d[i] = INF;
    for (int i = head[s]; i != -1; i = edge[i].next) {
        int v = edge[i].to;
        if (v == s) continue;
        d[v] = edge[i].dis;
        Q.push(v);
        vis[v] = 1;
    }
    while(!Q.empty()) {
        int u = Q.front();
        Q.pop();
        vis[u] = 0;
        for(int i = head[u]; i != -1; i = edge[i].next) {
            int v = edge[i].to;
            if(d[u] + edge[i].dis < d[v]) {
                d[v] = d[u] + edge[i].dis;
                if(!vis[v]) {
                    Q.push(v);
                    vis[v] = 1;
                }
            }
        }
    }
} 

void init() {
    en = 0;
    for (int i = 0; i <= n; i++) {
        vis[i] = 0;
        head[i] = -1;
    }
}

void input() {
    int a;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            scanf("%d", &a);
            addEdge(i, j, a);
        }
    }
}
void solve() {
    SPFA(1);
    int ans = d[n], temp1 = d[1];
    SPFA(n);
    int temp2 = d[n];
    printf("%d\n", min(ans, temp1 + temp2));
}

int main() {
    while (scanf("%d", &n) != EOF) {
        init();
        input();
        solve();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不可转载。

时间: 2024-10-09 10:24:00

hdu 4370 0 or 1 (最短路)的相关文章

HDU - 4370 0 or 1 最短路

HDU - 4370 参考:https://www.cnblogs.com/hollowstory/p/5670128.html 题意: 给定一个矩阵C, 构造一个A矩阵,满足条件: 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). 使得∑Cij*Xij(1<=i,j<=n)最小. 思路: 理解条件之前先

HDU 4370 0 or 1(最短路+最小环判断)

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 Description Given a n*n matrix Cij (1<=i,j<=n),We want to find a n*n matrix Xij (1<=i,j<

hdu 4370 0 or 1,最短路

题目描述 给定n * n矩阵C ij(1 <= i,j <= n),我们要找到0或1的n * n矩阵X ij(1 <= i,j <= n). 此外,X ij满足以下条件: 1.X 12 + X 13 + ... X 1n = 1 2.X 1n + X 2n + ... X n-1n = 1 3.对于每个i(1 <i <n),满足ΣXki(1 <= k <= n)=ΣXij(1 <= j <= n). 例如,如果n = 4,我们可以得到以下等式:

HDU 4370 0 or 1

0 or 1 Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 437064-bit integer IO format: %I64d      Java class name: Main Given a n*n matrix Cij (1<=i,j<=n),We want to find a n*n matrix $X_{ij} (1<=i,j<=n)

HDU 4370 0 or 1(spfa+思维建图+计算最小环)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4370 题目大意:有一个n*n的矩阵Cij(1<=i,j<=n),要找到矩阵Xij(i<=1,j<=n)满足以下条件: 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). 举个例子

(中等) HDU 4370 0 or 1,建模+Dijkstra。

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

HDU 4034 Graph(floyd,最短路,简单)

题目 一道简单的倒着的floyd. 具体可看代码,代码可简化,你有兴趣可以简化一下,就是把那个Dijsktra所实现的功能放到倒着的floyd里面去. #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int MAXN=110; const int INF=0x3f3f3f3f;//防止后面溢出,这个不能太大 bool vis[MAXN]; int pr

HDU 3832 Earth Hour (最短路)

Problem Description Earth Hour is an annual international event created by the WWF (World Wide Fund for Nature/World Wildlife Fund), held on the last Saturday of March, that asks households and businesses to turn off their non-essential lights and el

hdu 1690 Bus System(Dijkstra最短路)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690 Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6569    Accepted Submission(s): 1692 Problem Description Because of the huge popula