zoukankan      html  css  js  c++  java
  • POJ 1860 Currency Exchange 最短路 难度:0

    http://poj.org/problem?id=1860

    #include <cstdio>
    //#include <queue>
    //#include <deque>
    #include <cstring>
    using namespace std;
    #define MAXM 202
    #define MAXN 101
    int n,m;
    int first[MAXN];
    int next[MAXM];
    int pto[MAXM];
    double earn[MAXM];
    int start;
    double d[MAXN];
    double cost[MAXM];
    bool vis[MAXM];
    void addedge(int from,int to,double fromcost,double fromearn,int index){
        next[index]=first[from];
        pto[index]=to;
        cost[index]=fromcost;
        earn[index]=fromearn;
        first[from]=index;
    }
    bool input(){
        bool fl=false;
        memset(first,-1,sizeof(first));
       double double1;
        scanf("%d %d %d %lf",&n,&m,&start,&double1);
        d[start]=double1;
        double fromc,toc,frome,toe;
        int from,to;
        for(int i=0;i<m;i++){
            scanf("%d %d %lf %lf %lf %lf",&from,&to,&frome,&fromc,&toe,&toc);
            addedge(from,to,fromc,frome,2*i);
            addedge(to,from,toc,toe,2*i+1);
            if(from==start&&frome>0.999999){
                fl=true;
            }
            else if(to==start&&toe>0.999999){
                fl=true;
            }
        }
        return fl;
    }
    int que[MAXM];
    int quehead;
    int quetail;
    void pushback(int inse){
        que[quetail]=inse;
        quetail=(quetail+1)%MAXM;
        vis[inse]=true;
    }
    void pushfront(int inse){
        quehead=(quehead-1+MAXM)%MAXM;
        que[quehead]=inse;
        vis[inse]=true;
    }
    int pop(){
        int ans=que[quehead];
        quehead=(quehead+1)%MAXM;
        vis[ans]=false;
        return ans;
    }
    bool find_loop(){
        if(!input())return false;
        pushback(start);
        while(quehead!=quetail){
            int from=pop();
            int p=first[from];
            while(p!=-1){
                int to=pto[p];
                double dtemp1;
                if((dtemp1=(d[from]-cost[p])*earn[p])>d[to]){
                    d[to]=dtemp1;
                    if(!vis[to]){
                        if(d[to]>d[que[quehead]])pushfront(to);
                        else pushback(to);
                    }
                    if(to==start){
                        return true;
                    }
                }
                p=next[p];
            }
        }
        return false;
    }
    int main(){
        if(find_loop()){
            printf("YES
    ");
        }
        else printf("NO
    ");
    }
    

      

  • 相关阅读:
    Fedora安装ati显卡驱动
    [转]SQLite 3入门教程
    [转]QT:不规则窗口的实现
    Ubuntu 10.10可用源
    [转]Qtopia2.2.0移植
    [转]嵌入式Qtopia2.2.0开发环境的搭建和使用
    ES6 find 和 filter 的区别
    “/ArcGIS/rest”应用程序中的服务器错误——解决办法
    Java:String和Date、Timestamp之间的转换
    UVA 100 The 3n+1 Problem
  • 原文地址:https://www.cnblogs.com/xuesu/p/4754378.html
Copyright © 2011-2022 走看看