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.
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.
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 8Sample Output
NO YESHint
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");
}
}
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.
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.
3 2 1 20.0 1 2 1.00 1.00 1.00 1.00 2 3 1.10 1.00 1.10 1.00Sample 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");
}
}
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.
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.
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
Case 1: Yes Case 2: No
dis[v]<dis[u]*a
)不过这题的节点出现了字符串,我用的是map存起来然后再找,但是这样时间复杂度是很高的,应为map找比较费时,后来看到别人的自己定义了一个字符串数组来存放字符串,然后自定义一个函数来找字符串,这样会比较快。对比这两种方法,用map的时间是700ms左右,自己写函数的话是40ms左右

//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++);
}
}