hdu4370 最短路

题目链接在这里

问题描述

Given a n*n matrix Cij (1<=i,j<=n),We want to find a n*n matrix Xij (1<=i,j<=n),which is 0 or 1.

Besides,Xij meets the following conditions:
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).

For example, if n=4,we can get the following equality:

X12+X13+X14=1
X14+X24+X34=1
X12+X22+X32+X42=X21+X22+X23+X24
X13+X23+X33+X43=X31+X32+X33+X34

Now ,we want to know the minimum of ∑Cij*Xij(1<=i,j<=n) you can get.

Hint:
For sample, X12=X24=1,all other Xij 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 Cij(0<=Cij<=100000).

Output

For each case, output the minimum of ∑Cij*Xij 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

 

题目大意是给出一个n x n的矩阵C,让构建一个n x n的矩阵X,如果Xij为1的话,那么ans += Cij。说白了就是在矩阵C中选出几个点,求它们的和。但是这几个点不是随便找的,需要满足上面的三个条件。

这道题怎么也想不到是用最短路的方法解决的,开始看了邝斌巨犇的题解才懂。题解链接在这里

我们把C[i][j]当做从i点到j点的边的权值,即有一条边,它的起点是i,终点是j,权值是C[i][j],那么这个题目就可以转换成最短路问题了。

我们看题目给出的第一个条件

X12+X13+X14=1

这个的意思就是,从第1个点出发,只能到2、3、4……这几个点中的其中一个,换句话说第一个点的出度为1.

第二个条件也很明了,第n个点的入度为1

第三个条件的意思就是每个点的入度等于出度

这就很好做了,找一条从点1出发到达点n的最短路不就行了呗。

 

当然最短路只是情况1,还有另外一种情况:

点1出发走了一圈(未经过点n)又回来了,并且,点n出发走了一圈(未经过点1)又回来了,即有两个闭环。

 

两种情况所花费的权值取一个最小值输出就行了。

 

对于闭环的情况,我们可以在初始化时设dis[start] = INF,而start点所连接的点的权值dis[v] = C[start][v],这样就可以找闭环了。

 

代码如下:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MaxN = 310;
int dis[MaxN];
int cost[MaxN][MaxN];
int n;

void dijkstra(int st){
    priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > que;
    for(int v = 1; v <= n; ++v){
        if(v == st){        //这个是找闭环用的
            dis[v] = INF;
        }
        else{
            dis[v] = cost[st][v];
            que.push(make_pair(dis[v], v));
        }
    }

    while(!que.empty()){
        pair<int, int> p = que.top();
        que.pop();
        int u = p.second;
        if(dis[u] < p.first) continue;
        for(int v = 1; v <= n; ++v){
            if(dis[v] > dis[u] + cost[u][v]){
                dis[v] = dis[u] + cost[u][v];
                que.push(make_pair(dis[v], v));
            }
        }
    }
}

int main(){
    while(~scanf("%d", &n)){
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= n; ++j)
                scanf("%d", &cost[i][j]);
        dijkstra(1);
        int ans = dis[n];
        int loop1 = dis[1];
        dijkstra(n);
        int loopn = dis[n];
        printf("%d\n", min(ans, loop1 + loopn));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值