(中等) 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 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.

  神题,可以转化为最短路问题。这个题可以一点一点的分析,首先就是选了第一行的第k个之后,就要选一个第k行的,所以建边 1->k 边权为第一行第k个的值,然后求1->n的最短路就好。。。。。。

  不过这里还有一种特殊情况,就是1和其他形成一个环,N和其他形成一个环。所以答案就是两个环的和,和最短路中小的那一个。。。

代码如下:

#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 namespace std;

const int MaxN=400;
const int INF=10e8;

void Dijkstra(int cost[][MaxN],int lowcost[],int N,int start)
{
    priority_queue <int> que;
    int t;

    for(int i=1;i<=N;++i)
        lowcost[i]=INF;

    que.push(start);

    while(!que.empty())
    {
        t=que.top();
        que.pop();

        for(int i=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);
                }
    }
}

int map1[MaxN][MaxN];
int ans[MaxN];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int N;
    int t1,t2;

    while(~scanf("%d",&N))
    {
        for(int i=1;i<=N;++i)
            for(int j=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;
}

时间: 2024-11-03 03:27:04

(中等) HDU 4370 0 or 1,建模+Dijkstra。的相关文章

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

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: 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(最短路+最小环判断)

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 2066 一个人的旅行 Dijkstra

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2066 题意分析:以草儿家为原点,给出城市间相互抵达的时间,给出草儿想去的城市,求最短时间.典型的单源最短路问题,直接用Dijkstra即可,无需优化也可.需要注意的地方是:直接和草儿家相连的城市,时间可以算为0. 另外,题目强调可以有多条路径,所以要注意选取其中最短的一条. /*一个人的旅行 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 3

HDU 2112 HDU Today,最短路径算法,Dijkstra

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13396    Accepted Submission(s): 3144 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HD

HDU 1874-畅通工程续(Dijkstra+优先队列)

畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 28578    Accepted Submission(s): 10382 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行