都是问货币能不能增多,钻社会制度得空子啊哈哈
唯一不同得是你的起点是任意一个点,这个比较麻烦了,多了一层循环嘞
处理货币名可以用map分配id
然后就是老套的Billman_ford算法了
#include <iostream> #include <map> #include <vector> #include <string.h> #include <cstdio> #include <string> using namespace std; const int maxn = 40; map<string,int> id; double dis[maxn]; struct node{ int from,to; double v; node(int f,int t,double va):from(f),to(t),v(va){} node(){} };
这次联系了Stl中得容器,熟悉熟悉
int main() { int n,m; string s,a,b; double v; int cas = 1; while(~scanf("%d",&n),n) { id.clear(); edge.clear(); for(int i = 1;i <= n;i++) { cin>>s; id[s] = i; } scanf("%d",&m); while(m--) { cin>>a; scanf("%lf",&v); cin>>b; edge.push_back(node(id[a],id[b],v)); } printf("Case %d: ",cas++); for(int i = 1;i <= n;i++) { if(blm(i,n)) { printf("Yes "); break; } else if(i == n)printf("No "); } } return 0; }
vector<node> edge; bool blm(int s,int n) { for(int i = 1;i <= n;i++)dis[i] = 0; dis[s] = 1.0; for(int i = 1;i < n;i++) { for(int j = 0;j < edge.size();j++) { int from = edge[j].from; int to = edge[j].to; double v = edge[j].v; //cout<<from<<" "<<to<<" "<<v<<endl; dis[to] = max(dis[to],dis[from] * v); } //cout<<"cs"<<endl; } for(int i = 0;i < edge.size();i++) { int from = edge[i].from; int to = edge[i].to; double v = edge[i].v; if(dis[to] < dis[from] * v)return true; } return false; }