zoukankan      html  css  js  c++  java
  • 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
    ",min(ans,c1+c2));
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Webdriver API之元素定位
    学生XX码
    网站设计基础
    JS基础知识
    1、变量和基本类型
    网上地址下载图片
    网上地址下载图片2
    微信账号
    INSERT INTO SELECT语句与SELECT INTO FROM语句
    【基础知识】创建匹配游戏
  • 原文地址:https://www.cnblogs.com/KirisameMarisa/p/4297627.html
Copyright © 2011-2022 走看看