POJ 3259 Wormholes (贝尔曼算法判断负环) POJ 1860 Currency Exchange (判断正环) HDU 1217(贝尔曼判断正环)

本文探讨了通过特殊路径和虫洞实现时间旅行的可能性,并分析了利用不同货币间的汇率差异进行套利的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.

As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .

To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.

Input
Line 1: A single integer, F. F farm descriptions follow.
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2.. M+1 of each farm: Three space-separated numbers ( S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2.. M+ W+1 of each farm: Three space-separated numbers ( S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.
Output
Lines 1.. F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).
Sample Input
2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8
Sample Output
NO
YES
Hint
For farm 1, FJ cannot travel back in time.
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.

这题主要是理解题目意思:虫洞问题,现在有n个点,m条边,代表现在可以走的通路,比如从a到b和从b到a需要花费c时间,现在在地上出现了w个虫洞,虫洞的意义就是你从a到b话费的时间是-c(时间倒流,并且虫洞是单向的),现在问你从某个点开始走,能回到从前。也就是把农场的路看成是正权,虫洞看成是负权,然后判断一下是否存在负环就好了。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<vector>
using namespace std;
#define inf 0x3fffffff
const int N=510;
struct node
{
    int v,dis;
    node(int a,int b) : v(a),dis(b) {}//构造函数
};
vector<node> adj[N];//邻接表存图
int m,n,w,dis[N];
bool bell()
{
    bool flag;
    fill(dis,dis+N,inf);
    dis[0]=0;
    for(int i=0;i<n-1;i++)
    {
        flag=true;
        for(int u=0;u<n;u++)
        {
            for(int j=0;j<adj[u].size();j++)
            {
                int v=adj[u][j].v;
                int diss=adj[u][j].dis;
                if(dis[v]>diss+dis[u])
                {
                    dis[v]=diss+dis[u];
                    flag=false;
                }
            }
        }
        if(flag) return true;
    }
    for(int u=0;u<n;u++)
    {
        for(int j=0;j<adj[u].size();j++)
        {
            int v=adj[u][j].v;
            int diss=adj[u][j].dis;
            if(dis[v]>diss+dis[u])
                return false;//判断负环
        }
    }
    return true;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=0;i<n;i++)
            adj[i].clear();//一定要记得初始化
        int u,v,wt;
        scanf("%d%d%d",&n,&m,&w);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&wt);
            adj[u-1].push_back(node(v-1,wt));//双向边
            adj[v-1].push_back(node(u-1,wt));
        }
        for(int i=0;i<w;i++)
        {
            scanf("%d%d%d",&u,&v,&wt);
            adj[u-1].push_back(node(v-1,-wt));//单向负权
        }
        if(bell()) printf("NO\n");
        else printf("YES\n");
    }
}
Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency.
For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 - 0.39) * 29.75 = 2963.3975RUR.
You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B - numbers of currencies it exchanges, and real R AB, C AB, R BA and C BA - exchange rates and commissions when exchanging A to B and B to A respectively.
Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations.
Input
The first line of the input contains four numbers: N - the number of currencies, M - the number of exchange points, S - the number of currency Nick has and V - the quantity of currency units he has. The following M lines contain 6 numbers each - the description of the corresponding exchange point - in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=10 3.
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10 -2<=rate<=10 2, 0<=commission<=10 2.
Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 10 4.
Output
If Nick can increase his wealth, output YES, in other case output NO to the output file.
Sample Input
3 2 1 20.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00
Sample Output
YES

这一题跟上一题正好相反,这题是判断是否存在正环。
题目大意:n种货币,m个兑换点,每个兑换点可以将两种货币相互转化:A到B的汇率是29.75,手续费是0.39,那么你可以得到(100 - 0.39) * 29.75 = 2963.3975 B币。因为存在佣金的问题,所以可以使得一种货币经过若干次转换后变多。

//普通贝尔曼算法
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<vector>
using namespace std;
#define inf 0x3fffffff
const int N=110;
struct node
{
    int v;
    double a,b;
    node(int vv,double aa,double bb) : v(vv),a(aa),b(bb) {}
};
vector<node> adj[N];
int m,n,c1;
double dis[N],c2;
bool bell()
{
    bool flag;
    memset(dis,0,sizeof(dis));
    dis[c1-1]=c2;//存图的下标是从0开始的,所以初始化要c1-1
    for(int i=0;i<n-1;i++)
    {
        flag=true;
        for(int u=0;u<n;u++)
        {
            for(int j=0;j<adj[u].size();j++)
            {
                int v=adj[u][j].v;
                double a=adj[u][j].a;
                double b=adj[u][j].b;
                if(dis[v]<(dis[u]-b)*a)
                {
                    dis[v]=(dis[u]-b)*a;
                    flag=false;
                }
            }
        }
        if(flag) return true;
    }
    for(int u=0;u<n;u++)
    {
        for(int j=0;j<adj[u].size();j++)
        {
            int v=adj[u][j].v;
            double a=adj[u][j].a;
            double b=adj[u][j].b;
            if(dis[v]<(dis[u]-b)*a)
                return false;//判断是否存在正环
        }
    }
    return true;
}
int main()
{
    while(~scanf("%d%d%d%lf",&n,&m,&c1,&c2))
    {
        int x,y;
        double p,q,pp,qq;
        for(int i=0;i<n;i++)
            adj[i].clear();//记得要初始化
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%lf%lf%lf%lf",&x,&y,&p,&q,&pp,&qq);
            adj[x-1].push_back(node(y-1,p,q));
            adj[y-1].push_back(node(x-1,pp,qq));
        }
        if(bell()) printf("NO\n");
        else printf("YES\n");
    }
}
//队列优化后的贝尔曼算法
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<queue>
using namespace std;
const int N=110;
struct node
{
    int v;
    double a,b;
    node(int x,double y,double z) : v(x),a(y),b(z) {}
};
vector<node> adj[N];
int m,n,c1,num[N];
double dis[N],c2;
bool vis[N];
bool bell()
{
    memset(vis,false,sizeof(vis));
    memset(num,0,sizeof(num));
    memset(dis,0,sizeof(dis));
    queue<int> q;
    q.push(c1-1);
    vis[c1-1]=true;
    dis[c1-1]=c2;
    num[c1-1]++;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int j=0;j<adj[u].size();j++)
        {
            int v=adj[u][j].v;
            double a=adj[u][j].a;
            double b=adj[u][j].b;
            if(dis[v]<(dis[u]-b)*a)
            {
                dis[v]=(dis[u]-b)*a;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=true;
                    num[v]++;
                    if(num[v]>=n) return false;
                }
            }
        }
    }
    return true;
}
int main()
{
    while(~scanf("%d%d%d%lf",&n,&m,&c1,&c2))
    {
        int p,q;
        double rab,cab,rba,cba;
        for(int i=0;i<n;i++)
            adj[i].clear();
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%lf%lf%lf%lf",&p,&q,&rab,&cab,&rba,&cba);
            adj[p-1].push_back(node(q-1,rab,cab));
            adj[q-1].push_back(node(p-1,rba,cba));
        }
        if(!bell()) printf("YES\n");
        else printf("NO\n");
    }
}
Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5 percent.

Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not.
Input
The input will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible.
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes" respectively "Case case: No".
Sample Input
3
USDollar
BritishPound
FrenchFranc
3
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar

3
USDollar
BritishPound
FrenchFranc
6
USDollar 0.5 BritishPound
USDollar 4.9 FrenchFranc
BritishPound 10.0 FrenchFranc
BritishPound 1.99 USDollar
FrenchFranc 0.09 BritishPound
FrenchFranc 0.19 USDollar

0
Sample Output
Case 1: Yes
Case 2: No
这一题跟上一题应该是一样的了,都是货币之间的转换问题,这题应该更简单,这样写公式就好(dis[v]<dis[u]*a)不过这题的节点出现了字符串,我用的是map存起来然后再找,但是这样时间复杂度是很高的,应为map找比较费时,后来看到别人的自己定义了一个字符串数组来存放字符串,然后自定义一个函数来找字符串,这样会比较快。对比这两种方法,用map的时间是700ms左右,自己写函数的话是40ms左右再见再见这下算是感受到map的速度了
//map 719ms
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
using namespace std;
#define inf 0x3fffffff
const int N=35;
struct node
{
    int v;
    double a;
    node(int vv,double aa) : v(vv),a(aa) {}
};
map<string,int>mapp;
vector<node> adj[N];
int m,n;
double dis[N];
bool bell()
{
    memset(dis,0,sizeof(dis));
    dis[0]=1;
    for(int i=0;i<n-1;i++)
    {
        for(int u=0;u<n;u++)
        {
            for(int j=0;j<adj[u].size();j++)
            {
                int v=adj[u][j].v;
                double a=adj[u][j].a;
                if(dis[v]<dis[u]*a)
                    dis[v]=dis[u]*a;
            }
        }
    }
    for(int u=0;u<n;u++)
    {
        for(int j=0;j<adj[u].size();j++)
        {
            int v=adj[u][j].v;
            double a=adj[u][j].a;
            if(dis[v]<dis[u]*a)
                return false;
        }
    }
    return true;
}
int main()
{
    int k=1;
    string s,s1;
    double r;
    while(~scanf("%d",&n)&&n)
    {
        mapp.clear();//清空操作
        for(int i=0;i<N;i++)
            adj[i].clear();
        for(int i=0;i<n;i++)
        {
            cin>>s;
            mapp[s]=i;
        }
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            cin>>s>>r>>s1;
            adj[mapp[s]].push_back(node(mapp[s1],r));
        }
        if(!bell()) printf("Case %d: Yes\n",k++);
        else printf("Case %d: No\n",k++);
    }
}
//自定义函数查找  32ms
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
using namespace std;
#define inf 0x3fffffff
const int N=55;
struct node
{
    int v;
    double a;
    node(int vv,double aa) : v(vv),a(aa) {}
};
vector<node> adj[N];
int m,n;
double dis[N];
char nae[N][N];//二维数组存字符串
int finf( char s[] )
{
    for( int i=0 ; i<n ; i++ )
        if( !strcmp( nae[i] , s ) )
            return i;
}
bool bell()
{
    memset(dis,0,sizeof(dis));
    dis[0]=1;
    for(int i=0; i<n-1; i++)
    {
        for(int u=0; u<n; u++)
        {
            for(int j=0; j<adj[u].size(); j++)
            {
                int v=adj[u][j].v;
                double a=adj[u][j].a;
                if(dis[v]<dis[u]*a)
                    dis[v]=dis[u]*a;
            }
        }
    }
    for(int u=0; u<n; u++)
    {
        for(int j=0; j<adj[u].size(); j++)
        {
            int v=adj[u][j].v;
            double a=adj[u][j].a;
            if(dis[v]<dis[u]*a)
                return false;
        }
    }
    return true;
}
int main()
{
    int k=1;
    char s[N],s1[N];
    double r;
    while(~scanf("%d",&n)&&n)
    {
        memset(nae,0,sizeof(nae));
        for(int i=0; i<N; i++)
            adj[i].clear();
        for(int i=0; i<n; i++)
            scanf("%s",nae[i]);
        scanf("%d",&m);
        for(int i=0; i<m; i++)
        {
            scanf("%s%lf%s",s,&r,s1);
            int pp=finf(s),qq=finf(s1);//查找货币下标
            adj[pp].push_back(node(qq,r));
        }
        if(!bell()) printf("Case %d: Yes\n",k++);
        else printf("Case %d: No\n",k++);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值