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,我们可以得到以下等式:
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
现在,我们想知道你可以得到的最小ΣCij * X ij(1 <= i,j <= n)。
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 Cij(0<=Cij<=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

题目分析

关键是要理解输入的C是一个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).
第一条就是1到其余个点的出度为1
第二条个点到n的只有一个,就是n的入读为1
第三条其余各点出度入度相同。
叫你求乘积最小,于是便成为了最短路问题。从1到n,给定所有的距离,让你求出最短路问题(X是任意调配,只要满足三个条件就行,于是调配出最短路)

再来想象一下,一个图,n个点中,1号点出度为1,n号点入度为1,其余个点出度等于入度。其实每个点都能到其余个点,只不过这条边是由Xij控制,当其为1是这条边就显示出来了。要满足这三个条件,然后求处,显示出来的边长总和最短。

所以,可能还不单单只有一个最短路(最短路必定能够满足起点出度为1,终点入度为1,其余个点入度等于出度等于1)。
还有一种情况,1号点满足了出度为1,但它再来一个入度为1,同样,n号点也满足了入度为1,但是它再来一个出度为1。那么这就是不是从1到n的最短路了,这成了,1号点经过一些点成了一个闭环,n号点经过剩下的那些点成了一个闭环。这也可能是最短的,要与第一种情况做个比较才知道。
比如,有5个点,假设第一种情况下最短路为1-2-3-4-5。第二种闭环下的情况为,1-2-3-1和5-4-5两个闭环。显然,这两种情况都能满足X的三种条件。所以最终问题就是找到最短路与闭环最短路下谁的路径总和最短。

难题啊,还要多方面考虑呢。

采用SPFA算法。
关于SPFA,就是从起点开始,将与起点相连的点拉进队列中。
然后开始操作这个队列:不断地出去队首元素,如果dis[i]>dis[u]+cost[u][i],u为出去的队首元素,dis为起点到点i的最短距离。就是不断更新最短的距离,只要这个要出去的对手元素的点的最短距离加上它到达旁边i的边更小了,那么就更新一下。
最终,队列里所有元素都会出去,空了,就说明个点的扩散已经好了,更新不了了,那么最短距离就一定产生了。

代码

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>

using namespace std;

const int INF = 0x3f3f3f3f;

int cost[305][305];
int dis[305];
int n;
bool v[305]; //判断是否i点在队列中
int q[305];

void spfa(int s) {
    queue<int> q;
    for (int i = 1; i <= n; i++) {
        if (i == s) {
            dis[i] = INF;
            v[i] = 0;
        }else {
            dis[i] = cost[s][i];
            v[i] = 1;
            q.push(i);
        }
    }

    while (!q.empty()) {
        int u = q.front();
        q.pop();
        v[u] = 0;
        for (int i = 1; i <= n; i++) {
            if (dis[i] > dis[u] + cost[u][i]) {
                dis[i] = dis[u] + cost[u][i];
                if (v[i] == 0) {
                    v[i] = 1;
                    q.push(i);
                }
            }
        }

    }

}

int main() {
    while (scanf("%d", &n) != EOF) {
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                scanf("%d", &cost[i][j]);

        spfa(1);
        int ans = dis[n];
        int c1 = dis[1];
        spfa(n);
        int cn = dis[n];

        printf("%d\n", min(ans, c1 + cn));
    }
    return 0;
}

原文地址:https://www.cnblogs.com/wudongwei/p/9371037.html

时间: 2024-08-28 18:46:04

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 (最短路)

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

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