poj1860 Currency Exchange(java + bellman-Ford)
来源:互联网 发布:广州seo顾问 编辑:程序博客网 时间:2024/06/16 06:29
package test;
import java.util.Scanner;
/**问题请参考http://poj.org/problem?id=1860
* @author rayli
* @date:2014-7-27 下午4:16:26
* 题意 :有N种货币,同时有M个货币兑换站点,每个站点支持两种货币A,B之间的相互兑换,
* 六个数字(A,B,Rab,Cab,Rba,Cba)表示它的属性,
* 假如A->B,则A的量Va可换得的B货币数Vb则,Vb=(Va-Cab)*Rab;先有一个含有s种货币V元,问他能否经过各种兑换后,换得的s货币多于V元
*
*/
class exchange
{
public int a;//货币a
public int b;//货币b
public double r;// 汇率
public double c;//手续费
}
public class CurrencyExchange
{
exchange map[];
double dis[];
int n;
int s;
double v;
int e;//边
void input()
{
Scanner cin = new Scanner(System.in);
n = cin.nextInt();//货币种数
int m = cin.nextInt();//兑换点数量
map = new exchange[2*m];
dis = new double[2*m+1];//刚开始定义的是2m,但提交不过,所以后后定义数组时应该适当的开大点
s = cin.nextInt();//持有第s种货币
v = cin.nextDouble();//持有的s货币的本金
e = 0;
for(int i=0; i<m; i++)
{
int a = cin.nextInt();
int b = cin.nextInt();
map[e] = new exchange();
map[e].a = a;
map[e].b = b;
map[e].r = cin.nextDouble();
map[e].c = cin.nextDouble();
e++;
int tmp = e;
map[tmp] = new exchange();
map[tmp].a = b;
map[tmp].b = a;
map[tmp].r = cin.nextDouble();
map[tmp].c = cin.nextDouble();
e++;
}
cin.close();
}
boolean bellmanford()
{
dis[s] = v;
for(int i=1; i<=n; i++)
{
boolean flag = false;
for(int j=0; j<e; j++)
{
if(dis[map[j].b] < (dis[map[j].a] - map[j].c) * map[j].r)
{
dis[map[j].b] = (dis[map[j].a] - map[j].c) * map[j].r;
flag = true;
}
}
if(!flag)
break;
}
for(int k=0; k<e; k++)
{
if(dis[map[k].b] < (dis[map[k].a] - map[k].c) * map[k].r)
{
return true;
}
}
return false;
}
void output()
{
if(bellmanford())
System.out.println("YES");
else
System.out.println("NO");
}
public static void main(String args[])
{
CurrencyExchange ce = new CurrencyExchange();
ce.input();
ce.output();
}
}
import java.util.Scanner;
/**问题请参考http://poj.org/problem?id=1860
* @author rayli
* @date:2014-7-27 下午4:16:26
* 题意 :有N种货币,同时有M个货币兑换站点,每个站点支持两种货币A,B之间的相互兑换,
* 六个数字(A,B,Rab,Cab,Rba,Cba)表示它的属性,
* 假如A->B,则A的量Va可换得的B货币数Vb则,Vb=(Va-Cab)*Rab;先有一个含有s种货币V元,问他能否经过各种兑换后,换得的s货币多于V元
*
*/
class exchange
{
public int a;//货币a
public int b;//货币b
public double r;// 汇率
public double c;//手续费
}
public class CurrencyExchange
{
exchange map[];
double dis[];
int n;
int s;
double v;
int e;//边
void input()
{
Scanner cin = new Scanner(System.in);
n = cin.nextInt();//货币种数
int m = cin.nextInt();//兑换点数量
map = new exchange[2*m];
dis = new double[2*m+1];//刚开始定义的是2m,但提交不过,所以后后定义数组时应该适当的开大点
s = cin.nextInt();//持有第s种货币
v = cin.nextDouble();//持有的s货币的本金
e = 0;
for(int i=0; i<m; i++)
{
int a = cin.nextInt();
int b = cin.nextInt();
map[e] = new exchange();
map[e].a = a;
map[e].b = b;
map[e].r = cin.nextDouble();
map[e].c = cin.nextDouble();
e++;
int tmp = e;
map[tmp] = new exchange();
map[tmp].a = b;
map[tmp].b = a;
map[tmp].r = cin.nextDouble();
map[tmp].c = cin.nextDouble();
e++;
}
cin.close();
}
boolean bellmanford()
{
dis[s] = v;
for(int i=1; i<=n; i++)
{
boolean flag = false;
for(int j=0; j<e; j++)
{
if(dis[map[j].b] < (dis[map[j].a] - map[j].c) * map[j].r)
{
dis[map[j].b] = (dis[map[j].a] - map[j].c) * map[j].r;
flag = true;
}
}
if(!flag)
break;
}
for(int k=0; k<e; k++)
{
if(dis[map[k].b] < (dis[map[k].a] - map[k].c) * map[k].r)
{
return true;
}
}
return false;
}
void output()
{
if(bellmanford())
System.out.println("YES");
else
System.out.println("NO");
}
public static void main(String args[])
{
CurrencyExchange ce = new CurrencyExchange();
ce.input();
ce.output();
}
}
阅读全文
0 0
- poj1860 Currency Exchange(java + bellman-Ford)
- poj1860 Currency Exchange --- Bellman-Ford
- Poj1860 Currency Exchange Bellman-Ford
- POJ1860《Currency Exchange》方法:反向Bellman-ford
- POJ1860 Currency Exchange(Bellman-ford的变形)
- POJ1860 Currency Exchange 反向bellman-ford
- POJ1860 Currency Exchange 最短路bellman-ford
- Bellman-ford变形 poj1860 Currency Exchange
- POJ1860(Currency Exchange)(Bellman-Ford)
- POJ1860-Currency Exchange(bellman-ford求正环)
- POJ1860 Currency Exchange【bellman-Ford算法】
- poj1860 Currency Exchange bfs剪枝 / bellman-ford 单源最短路径 初学
- Currency Exchange 反向Bellman-Ford
- Currency Exchange(Bellman-Ford)
- Currency Exchange(bellman-Ford)
- 2011.11.3 poj1860 Currency Exchange 单源最短路径 Bellman-Ford算法 解题报告
- poj1860 Currency Exchange(bellman+spfa解法)
- pku 1860 Currency Exchange(Bellman-Ford )
- poj1328 Radar Installation(java)
- poj2109 Power of Cryptography(java)
- poj2586 Y2K Accounting Bug(贪心,java)
- poj 1573 Robot Motion(java 模拟)
- poj 1125 Stockbroker Grapevine
- poj1860 Currency Exchange(java + bellman-Ford)
- poj2485 Highways(prim java)
- poj 3026 Borg Maze(java bfs + prim)
- poj 1258 Agri-Net( java prim)
- poj 1789 Truck History(java + prim)
- poj 1094 Sorting It All Out(java + 拓扑排序)
- poj 3020 Antenna Placement(java + 匈牙利算法)
- Java项目经验——程序员成长的钥匙
- poj 3041 Asteroids(java + 匈牙利算法)