floyd
对于floyd的一点理解。
首先对于floyd;是借助动态规划的思想得到的。
d
p
[
k
]
[
i
]
[
j
]
dp[k][i][j]
dp[k][i][j]:表示经过前面k个点的
,两两之间的最短路。
Arbitrage(poj 2240)
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.
our 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
思路:
- 首先读入数据,在读入m个rate时,要读取字符串,判断前面是否有出现相同的,并找到n个里的编号具体看代码。
- 之后题意要求你找到一个正环。
AC
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define For(i,x,y) for(register int i=(x); i<=(y);i++)
using namespace std;
const int INF=1e9;
double g[40][40];
char s[40][1000];
int n,m;
int main()
{
int kase=0;
while(scanf("%d", &n)&&n)
{
For(i,1,n)
{
For(j,1,n)(i==j)?g[i][j]=1:g[i][j]=0;
//初始化
}
int flag=0;
For(i,1,n)scanf("%s",s[i]);
scanf("%d",&m);
char w[1000];
For(i,1,m)
{
int x,y;
double rate;
scanf("%s",w);
int j=1;
while(strlen(s[j])>0&&strcmp(s[j],w)!=0)j++;
//数据读入,并找到这种钱币的编号
x=j;
scanf("%lf",&rate);
scanf("%s",w);
j=1;
while(strlen(s[j])>0&&strcmp(s[j],w)!=0)j++;
y=j;
g[x][y]=rate;
}
For(k,1,n)
{
For(i,1,n)
{
For(j,1,n)
{
if(g[i][j]<g[i][k]*g[k][j])
{
g[i][j]=g[i][k]*g[k][j];
//cout<<i<<' '<<k<<' '<<j<<endl;
// cout<<g[i][j]<<endl<<endl;
if(g[i][j]>1&&i==j)flag=1;
}
if(flag)break;
}
if(flag)break;
}
if(flag)break;
}
printf("Case %d: ",++kase);
if(flag)printf("Yes\n");
else printf("No\n");
}
return 0;
}
Tram(poj1847)
思路:
通向默认路口的路径权值为0,通向其它路口的路径权值为1,然后求解最短路即可。
AC
#include <iostream>
#include <cstring>
#define For(i,x,y) for(register int i=(x); i<=(y); i++)
using namespace std;
const int INF=0x3f3f3f3f;
int g[110][110];
int main()
{
int n,a,b;
cin>>n>>a>>b;
For(i,1,n)
{
For(j,1,n)g[i][j]=(i==j)?0:INF;
}
For(i,1,n)
{
int k,tmp1=0;
cin>>k;
For(j,1,k)
{
int tmp;
cin>>tmp;
g[i][tmp]=tmp1;
tmp1=1;
}
}
For(k,1,n)
{
For(i,1,n)
{
For(j,1,n)
{
g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
}
}
}
if(g[a][b]!=INF)cout<<g[a][b]<<endl;
else cout<<-1<<endl;
return 0;
}