SFPA求非自环闭环的最短路

题目链接:题目传送门

 

题目大意:

 

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

 

 

 

思路就不细说了,转自网上解释:

 

此题非常厉害,就算法来说不是很难,但很考想法。这题的精髓在于如何将条件抽象出来,并建图。

三个条件分别告诉我们 点1的出度为1,点n的入度为1,每个点的入度等于出度,这样就可以构图了。设n各点,将∑Cij*Xij(1<=i,j<=n)看成n各点的邻接矩阵。这时问题分成了两种情况。

第一种是求1到n的最短路径,第二种是求1到1的非自环闭环 + n到n的非自环闭环的和。比较这两种情况,取最小的值为解。

用spfa时设d[s]的值为INF,让d[s]的相邻节点入栈,这样保证了可以取到非自环闭环。

 

代码:

 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include<malloc.h>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#define ri(n) scanf("%d",&n)
#define oi(n) printf("%d\n",n)
#define rl(n) scanf("%lld",&n)
#define ol(n) printf("%lld\n",n)
#define rep(i,l,r) for(i=l;i<=r;i++)
#define rep1(i,l,r) for(i=l;i<r;i++)
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int epg=10-8;
const int maxn=1000;
int n,m;//顶点数,边数
int e[maxn][maxn];
int a[maxn][maxn];
int vis[maxn],d[maxn],c[maxn],path[maxn];
//int n;
bool SPFA(int s)
{
    queue<int>p;
    //p.clear();
    while(!p.empty())
        p.pop();
    for(int i=1; i<=n; i++)
    {
        //path[i]=s;
        //vis[i]=0;
        if(i!=s)
        {
            d[i]=a[s][i];
            vis[i]=1;
            p.push(i);
        }
        else
        {
            d[i]=inf;
            vis[i]=0;
        }

    }
    //vis[s]=1;
   // c[s]++;
    //d[s]=0;
    while(!p.empty())
    {
        int u=p.front();
        p.pop();
        vis[u]=0;
        for(int v=1; v<=n; v++)
        {
            //if(e[u][v]!=inf)//如果u点和i点直接相邻,就是u,i点之间有边
            //{
                if(d[v]>d[u]+a[u][v])
                {
                    d[v]=d[u]+a[u][v];
                   // path[v]=u;
                    if(!vis[v])
                    {
                        p.push(v);
                        //c[v]++;
                        //if(c[v]>=n)
                            //return false;
                        vis[v]=1;
                    }
                }
            //}
        }
    }
    //return true;
}
int main()
{
    //int n;
    while(scanf("%d",&n)==1)
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            scanf("%d",&a[i][j]);
        SPFA(1);
        int b1=d[n],b2=d[1];
        SPFA(n);
        b2+=d[n];
        int b3=min(b1,b2);
        printf("%d\n", b3);
    }
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值