hdu4370 0 or 1【最短路+建图】

转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297627.html   ---by 墨染之樱花

 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4370

题目描述:正如字面意思

思路:相当的鬼畜orz。。。开始只想到了看做邻接矩阵,应该是直接跑一下SPFA的事情,结果过了样例挂了judge,后来参考了邝巨巨的博客http://www.cnblogs.com/kuangbin/archive/2012/08/17/2644557.html之后才知道还有最小花费环的情况B。详情思路请见邝神博客

 

#include <iostream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <climits>
using namespace std;
#define XINF INT_MAX
#define INF 1<<30
#define MAXN 300+10
#define eps 1e-8
#define zero(a) fabs(a)<eps
#define sqr(a) ((a)*(a))
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define PF(X) push_front(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
#define PI  acos(-1.0)
#define test puts("OK");
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
typedef pair<int,int> PII;
typedef priority_queue<int,vector<int>,greater<int> > PQI;
typedef vector<PII> VII;
typedef vector<int> VI;
#define X first
#define Y second

int V,E;
int cost[MAXN][MAXN];
int d[MAXN];
bool inq[MAXN];

void SPFA(int s)
{
    queue<int> Q;
    REP(i,V)
    {
        if(i==s)
        {
            d[i]=INF;
            inq[i]=0;
        }
        else
        {
            d[i]=cost[s][i];
            Q.push(i);
            inq[i]=1;
        }
    }
    while(!Q.empty())
    {
        int u=Q.front();Q.pop();inq[u]=0;
        REP(v,V)
        {
            if(d[v]>d[u]+cost[u][v])
            {
                d[v]=d[u]+cost[u][v];
                if(!inq[v])
                {
                    Q.push(v);
                    inq[v]=1;
                }
            }
        }
    }
} 

int main()
{_
    while(~scanf("%d",&V))
    {
        REP(i,V)
            REP(j,V)
                scanf("%d",&cost[i][j]);
        SPFA(0);
        int ans=d[V-1],c1=d[0];
        SPFA(V-1);
        int c2=d[V-1];
        printf("%d\n",min(ans,c1+c2));
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/KirisameMarisa/p/4297627.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值