POJ 1860 Currency Exchange(spfa)

匿名 (未验证) 提交于 2019-12-02 22:56:40
版权声明:听说这里让写版权声明~~~ https://blog.csdn.net/m0_37691414/article/details/82054152

解析:判断是否存在回路。

 #include<iostream> #include<algorithm> #include<vector> #include<queue> #include<cstdio> using namespace std; const int maxn = 110; struct node{ 	int to; 	double r, c; }; double dis[maxn]; int cnt[maxn]; bool visit[maxn]; vector<node> e[maxn]; int n, m, s; double v; bool solve(){ 	queue<int> q; 	q.push(s); 	dis[s] = v; 	visit[s] = true; 	cnt[s]++; 	while(!q.empty()){ 		int from = q.front(); 		q.pop(); 		visit[from] = false; 		for(int i = 0; i < e[from].size(); ++i){ 			node &P = e[from][i]; 			int to = P.to; 			double r = P.r, c = P.c; 			if(dis[to] < (dis[from] - c)*r){ 				dis[to] = (dis[from] - c)*r; 				if(visit[to] == false) 					visit[to] = true, q.push(to), cnt[to]++; 				if(cnt[to] > n)	return true; 			} 		} 	} 	return false; } int main(){ 	scanf("%d%d%d%lf", &n, &m, &s, &v); 	int from, to; 	double r, c; 	for(int i = 0; i < m; ++i){ 		scanf("%d%d%lf%lf", &from, &to, &r, &c); 		e[from].push_back((node){to, r, c}); 		scanf("%lf%lf", &r, &c); 		e[to].push_back((node){from, r, c}); 	} 	if(solve())	printf("YES\n"); 	else printf("NO\n"); 	return 0; }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!