HDU-4370 '0 or 1' 最短路 要考虑连通性

题目链接:https://cn.vjudge.net/problem/HDU-4370

题意

给一个矩阵C(nn),要我们找到一个矩阵X(nn),满足以下条件:

X_{12}+X_{13}+...X_{1n}=1

X_{1n}+X_{2n}+...X_{n-1n}=1

for each i (1<i<n), satisfies ∑X_{ki} (1<=k<=n)=∑X_{ij} (1<=j<=n).

min ∑C ij*X ij

思路

如果把X当成一个邻接矩阵,可以发现本题就是要找一个图,满足以下条件:

  1. 节点1有一个出度(注意不要1->1,因为要最小化边权),节点n有一个入度(同理不要n->n)
  2. 其他节点出度等于入度
  3. 最小化边权

很容易发现最短路是一种可能的情况(每个节点仅有一个出度入度)

另外还有一种情况需要考虑,就是起点和终点可以不连通,意思就是节点1节点n各参与一个互不连通的环

这还是要考虑连通问题啊

又没考虑,可烦,考虑开一个最短路专题总结一下

提交过程

WA1 没考虑连通性
WA2 int换long long
WA3 脑抽加上了1->1和n->n情况

代码

#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=320;
const long long INF=1LL<<60;
typedef pair<long long, int> Node;
struct Cmp{
    bool operator () (const Node &a, const Node &b){
        return a.first>b.first;
    }
};
int G[maxn+5][maxn+5];

long long Dij(int n){
    long long dist[maxn+5], ans=0, circle1=INF, circle2=INF;
    priority_queue<Node, vector<Node>, Cmp> que;

    for (int i=0;i<=n; i++) dist[i]=INF;
    dist[1]=0;
    que.push(Node(dist[1], 1));
    while (que.size()){
        Node x=que.top(); que.pop();
        if (x.first!=dist[x.second]) continue;

        int &from=x.second;
        for (int to=1; to<=n; to++) if (to!=from){
            int &dis=G[from][to];

            if (to==1) circle1=min(circle1, dist[from]+dis);
            if (dist[to]<=dist[from]+(long long)dis) continue;
            dist[to]=dist[from]+(long long)dis;
            que.push(Node(dist[to], to));
        }
    }//return dist[n];

    ans=dist[n];
    for (int i=0;i<=n; i++) dist[i]=INF;
    dist[n]=0;
    que.push(Node(dist[n], n));
    while (que.size()){
        Node x=que.top(); que.pop();
        if (x.first!=dist[x.second]) continue;

        int &from=x.second;
        for (int to=1; to<=n; to++) if (to!=from){
            int &dis=G[from][to];

            if (to==n) circle2=min(circle2, dist[from]+dis);
            if (dist[to]<=dist[from]+(long long)dis) continue;
            dist[to]=dist[from]+(long long)dis;
            que.push(Node(dist[to], to));
        }
    }return min(ans, circle1+circle2);
}

int main(void){
    int n;

    while (scanf("%d", &n)==1 && n){
        for (int y=1; y<=n; y++)
            for (int x=1; x<=n; x++) scanf("%d", &G[y][x]);
        printf("%lld\n", Dij(n));
    }

    return 0;
}
Time Memory Length Lang Submitted
1107ms 2012kB 1790 G++ 2018-06-02 11:28:23

HDU-4370 '0 or 1' 最短路 要考虑连通性

原文地址:https://www.cnblogs.com/tanglizi/p/9158868.html

时间: 2024-11-05 02:23:35

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

题目描述 给定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