POJ 1860 Currency Exchange(最短路&spfa正权回路)题解

题意:n种钱,m种汇率转换,若ab汇率p,手续费q,则b=(a-q)*p,你有第s种钱v数量,问你能不能通过转化让你的s种钱变多?

思路:因为过程中可能有负权值,用spfa。求是否有正权回路,dis[s]是否增加。把dis初始化为0,然后转化,如果能增大就更新。每次都判断一下dis[s]。

参考:最快最好用的——spfa算法

代码:

#include<cstdio>
#include<set>
#include<vector>
#include<cmath>
#include<queue>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = 100+5;
const int INF = 0x3f3f3f3f;
struct Edge{
    int v;
    double cost,dec;
    Edge(int _v = 0,double _c = 0,double _d = 0):v(_v),cost(_c),dec(_d){}
};
vector<Edge> G[maxn];
bool vis[maxn]; //在队列标志
//int cnt[maxn];  //每个点入队列次数
double dis[maxn];
bool spfa(int start,double V,int n){
    memset(vis,false,sizeof(vis));
    for(int i = 1;i <= n;i++) dis[i] = 0;
    vis[start] = true;
    dis[start] = V;
    queue<int> q;
    while(!q.empty()) q.pop();
    q.push(start);
    //memset(cnt,0,sizeof(cnt));
    //cnt[start] = 1;
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(int i = 0;i < G[u].size();i++){
            int v = G[u][i].v;
            double c = G[u][i].cost;
            double d = G[u][i].dec;
            if(dis[v] < (dis[u] - d)*c){
                dis[v] = (dis[u] - d)*c;
                if(!vis[v]){
                    q.push(v);
                    vis[v] = true;
                    /*if(++cnt[v] > n)
                        return false;*/
                }
            }
            if(dis[start] > V)
                return true;
        }
    }
    return false;
}
void addEdge(int u,int v,double cost,double dec){
    G[u].push_back(Edge(v,cost,dec));
}
int main(){
    int n,m,s;
    double V;
    while(scanf("%d%d%d%lf",&n,&m,&s,&V) != EOF){
        for(int i = 1;i <= n;i++) G[i].clear();
        for(int i = 0;i < m;i++){
            int u,v;
            double c,d;
            scanf("%d%d%lf%lf",&u,&v,&c,&d);
            addEdge(u,v,c,d);
            scanf("%lf%lf",&c,&d);
            addEdge(v,u,c,d);
        }
        bool flag = spfa(s,V,n);
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

 

posted @ 2018-08-01 11:19  KirinSB  阅读(164)  评论(0编辑  收藏  举报