POJ 1860: Currency Exchange:bellman 최단거리 변형으로 이익 판정 링
Time Limit: 1000MS
Memory Limit: 30000K
Total Submissions: 19064
Accepted: 6814
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
Source
Northeastern Europe 2001, Northern Subregion
도론의 입문은 이윤의 고리가 존재하지 않는다고 판단한다.반복 검색.간단명료한 폭력 수색.
이 문제의 표준 해법은bellman_ford의 최단로 변형은 비교적 어렵다.원래의 알고리즘은 마이너스 회로가 있으면 가장 짧은 n-1회 이완 후 수렴하지 않는다는 것이다.이 문제의 상황은 정반대입니다. 회로의 값이 한 바퀴 한 바퀴 증가하면bellman_ford 알고리즘은 가장 긴 경로를 구하고 가장 긴 길의 가치는 n-1회 이완된 후에도 수렴하지 않아 이윤 회로가 있다고 판단한다.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int n,m,s,ta,tb;
double v,r[105][105],c[105][105],use[105];
int result;
void init()
{
int i,j;
for(i=0;i<105;i++)
{
for(j=0;j<105;j++)
r[i][j]=c[i][j]=-1;
use[i]=-1;
}
for(i=1;i<=m;i++)
{
scanf("%d%d%",&ta,&tb);
scanf("%lf%lf%lf%lf",&r[ta][tb],&c[ta][tb],&r[tb][ta],&c[tb][ta]);
}
result=0;
}
void search(int x,double y)
{
int i,j;
if(result||y<=0)
return ;
if(use[x]!=-1)
{
if(y>use[x])
{
result=1;
return ;
}
}
else
{
use[x]=y;
for(i=1;i<=n;i++)
{
if(r[x][i]!=-1)
search(i,(use[x]-c[x][i])*r[x][i]);
}
use[x]=-1;
}
}
int main()
{
int i,j;
while(scanf("%d%d%d%lf",&n,&m,&s,&v)!=EOF)
{
init();
search(s,v);
if(result)
printf("YES
");
else
printf("NO
");
}
return 0;
}
bellman_ford 개선 알고리즘 구현:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int n,m,s,ta,tb;
double v,r[105][105],c[105][105],use[105],d[105];
int result;
void init()
{
int i,j;
for(i=0;i<105;i++)
{
for(j=0;j<105;j++)
r[i][j]=c[i][j]=-1;
use[i]=-1;
d[i]=0;
}
for(i=1;i<=m;i++)
{
scanf("%d%d%",&ta,&tb);
scanf("%lf%lf%lf%lf",&r[ta][tb],&c[ta][tb],&r[tb][ta],&c[tb][ta]);
}
result=0;
}
void bellman()
{
int i,j,k;
d[s]=v;
for(k=0;k<n-1;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(r[i][j]!=-1&&d[i]>c[i][j]&&d[j]<(d[i]-c[i][j])*r[i][j])
d[j]=(d[i]-c[i][j])*r[i][j];
}
}
}
for(i=1;i<=n;i++)
{
if(result)
break;
for(j=1;j<=n;j++)
{
if(r[i][j]!=-1&&d[i]>c[i][j]&&d[j]<(d[i]-c[i][j])*r[i][j])
{
result=1;
break;
}
}
}
}
int main()
{
int i,j;
while(scanf("%d%d%d%lf",&n,&m,&s,&v)!=EOF)
{
init();
bellman();
if(result)
printf("YES
");
else
printf("NO
");
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.