Arbitrage POJ - 2240【最短路】

Arbitrage POJ - 2240

题目

相似题目

思路:spfa判断正环即可,用map把图建立出来

注意:各种数组一定要初始化…

具体代码如下

#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<cstring>

using namespace std;

const int N = 50;

int n, m, cnt;
int ans[N];
double g[N][N];
double dis[N];
bool st[N];
map<string, int> S;

bool spfa(){
	memset(ans, 0, sizeof ans);
	memset(dis, 0, sizeof dis);
	memset(st, 0, sizeof st);
	dis[1] = 1000;
	queue<int> q;
	q.push(1);
	st[1] = true;
	while(q.size()){
		int t = q.front();
		q.pop();
		st[t] = false;
		for(int i=1; i<=n; ++i)
			if(g[t][i] != -1 && dis[i] < dis[t]*g[t][i]){
				dis[i] = dis[t] * g[t][i];
				ans[i] = ans[t] + 1;
				if(!st[i]){
					st[i] = true;
					q.push(i);
				}
				if(ans[i] >= n) return true;
			}
	}
	return false;
}

int main(){
	
	int kase = 0;
	while(cin >> n, n){
		cnt = 1;
		S.clear();
		memset(g, -1, sizeof g);
		for(int i=0; i<n; ++i){
			string s;
			cin >> s;
			S[s] = cnt++;
		}
		
		cin>>m;
		for(int i=0; i<m; ++i){
			string a, c; double b;
			cin >> a >> b >> c;
			g[S[a]][S[c]] = b;
		}

		
		
		if(spfa()) printf("Case %d: Yes\n", ++kase);
		else printf("Case %d: No\n", ++kase);
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值