Currency Exchange(최단거리 + Bellman Ford)

7514 단어 Exchange

Currency Exchange
Time Limit: 1000MS
 
Memory Limit: 30000K
Total Submissions: 17890
 
Accepted: 6314
Description
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(1~100), M(1~100), S(1~N), V(0~1000)를 제시하면 각각 N종의 화폐, M조의 화폐 관계, 현재 가지고 있는 화폐의 종류 S, 현재 가지고 있는 화폐의 종류 S의 총액을 나타낸다.다음에 M 점프 관계를 제시하면 각 관계는 모두 6개의 수가 있는데 먼저 A, B를 제시하면 이것은 A와 B의 직접적인 관계임을 나타낸다.뒤에 네 개의 수Rab,Cab,Rba,Cba가 있는데 각각 전자가 후자를 바꾸는 비율과 세제를 대표한다. 예를 들어 A가 B로 바꾸려면 B=(A-Cab)*Rab이다.S코인이 V보다 크더라도 최초로 보유한 S코인을 평가절상시킬 수 있는 방법이 있느냐고 물었다.
 
생각:
최단로.Bellman Ford는 정환이 있는지, 시간 조건이 교체된 묘사법을 충족하는지 판단한다.
제목을 이해한 지 오래되었습니다. Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. 이 말은 노드당 최대 1회만 사용할 수 있다고 생각했기 때문에 생동감 있게 분석한 결과 예시를 내지 못했습니다. 나중에 여러 번 반복해서 운용한 후에 평가절상할 수 있다는 것을 알게 되었습니다.나중에 제목을 이해한 후에 Bellman Ford를 사용하려고 했는데 Dijkstra를 SPFA로 사용해서 AC를 했지만 코드가 약간 추해서 Bellman Ford를 간결하게 교체하는 방법을 썼다.
 
    AC:
    Once:
#include <cstdio>
#include <queue>
#include <utility>
#include <iostream>
#include <vector>
#include <string.h>
#define MAX 300
using namespace std;

typedef pair<double,int> pii;
typedef struct {
    double r,c;
}edge;

edge w[MAX];
double d[105],mon;
int fir[105],v[MAX],next[MAX],vis[105],time[105];
int n,m,s,ind;

double calculate(double mon,edge a) {
    return (mon - a.c) * a.r;
}

void add_edge(int f,int t,double r,double c) {
    v[ind] = t;
    w[ind].r = r;
    w[ind].c = c;
    next[ind] = fir[f];
    fir[f] = ind;
    ind++;
}

int Dijkstra() {
    memset(d,0,sizeof(d));
    memset(vis,0,sizeof(vis));
    memset(time,0,sizeof(time));
    d[s] = mon;
    priority_queue<pii,vector<pii>,greater<pii> > q;
    q.push(make_pair(d[s],s));
    time[s]++;
    while(!q.empty()) {
        pii k = q.top();q.pop();
        int x = k.second;
        vis[x] = 0;
        for(int e = fir[x];e != -1;e = next[e]) {
            int y = v[e];
            double res = calculate(d[x],w[e]);
            if(d[y] < res) {
               d[y] = res;
               if(!vis[y]) {
                    q.push(make_pair(d[y],y));
                    time[y]++;
                    vis[y] = 1;
                    if(time[y] > n) return 1;
               }
            }
        }
    }

    return 0;
}

int main() {
    scanf("%d%d%d%lf",&n,&m,&s,&mon);
    ind = 0;
    memset(fir,-1,sizeof(fir));
    while(m--) {
        int a,b;
        double r,c;
        scanf("%d%d%lf%lf",&a,&b,&r,&c);
        add_edge(a,b,r,c);
        scanf("%lf%lf",&r,&c);
        add_edge(b,a,r,c);
    }

    if(Dijkstra())  puts("YES");
    else            puts("NO");

    return 0;
}

 
    Twice:
#include <stdio.h>
#include <string.h>
#define MAX 500

typedef struct {
    double r,c;
}node;

node w[MAX];
int u[MAX],v[MAX];
double d[205];
int n,ind;

void add_edge(int f,int t,double r,double c) {
    u[ind] = f;
    v[ind] = t;
    w[ind].r = r;
    w[ind].c = c;
    ind++;
}

double cal(double x,node a) {
    return (x - a.c) * a.r;
}

int Bellman_ford(int s,double mon) {
    memset(d,0,sizeof(d));
    d[s] = mon;
    for(int i = 1;i < n;i++)
        for(int j = 0;j < ind;j++){
        int x = u[j],y = v[j];
        double res = cal(d[x],w[j]);
        if(d[x] > 0 && d[y] < res)  d[y] = res;
    }

    for(int j = 0;j < ind;j++) {
        int x = u[j],y = v[j];
        double res = cal(d[x],w[j]);
        if(d[y] < res)  return 1;
    }

    return 0;
}

int main() {
    int m,s;
    double mon;
    scanf("%d%d%d%lf",&n,&m,&s,&mon);
    ind = 0;
    while(m--) {
        int a,b;
        double r,c;
        scanf("%d%d%lf%lf",&a,&b,&r,&c);
        add_edge(a,b,r,c);
        scanf("%lf%lf",&r,&c);
        add_edge(b,a,r,c);
    }

    if(Bellman_ford(s,mon))  puts("YES");
    else    puts("NO");
    return 0;
}

좋은 웹페이지 즐겨찾기