首页 > 代码库 > POJ 1860 Currency Exchange
POJ 1860 Currency Exchange
/**POJ 1860 Currency Exchange*判断回路是否能使初始值增长*/#include <cstdio>#include <cstring>#include <queue>using namespace std;#define INF 0x3f3f3f3f#define MAXN 600struct ArcNode { int to; double r; double c; ArcNode *next;};queue<int> q;int N, M, S;double V;ArcNode *list[MAXN]; //每个顶点的边链表表头指针bool visit[MAXN]; //每个顶点是否在队列int count[MAXN]; //每个顶点入队列的次数double dist[MAXN];bool spfa(int src){ int i, u, v; ArcNode *temp; for (i = 1 ; i <= N; i++) { dist[i] = 0; } dist[src] = V; q.push(src); while ( !q.empty()) { if(dist[src] > V) return true; u = q.front(); q.pop(); temp = list[u]; while (temp != NULL) { v = temp->to; if (dist[v] < (dist[u] - temp->c) * temp->r) { dist[v] = (dist[u] - temp->c) * temp->r; if (!visit[v]) { q.push(v); visit[v] = 1; } count[v]++; if (count[v] > N) //存在回路 return true; } temp = temp->next; } visit[u] = 0; } return false;}int main(){ int i, T; int u, v; double r, c; while (~scanf("%d %d %d %lf", &N, &M, &S, &V)) { memset(list, 0, sizeof(list)); memset(visit, 0, sizeof(visit)); memset(count, 0, sizeof(count)); while (!q.empty()) { q.pop(); } ArcNode *temp; for (i = 0; i < M; i++) { scanf("%d %d %lf %lf", &u, &v, &r, &c); temp = new ArcNode; temp->to = v; temp->r = r; temp->c = c; temp->next = NULL; if (list[u] == NULL) { list[u] = temp; } else { temp->next = list[u]; list[u] = temp; } scanf("%lf %lf", &r, &c); temp = new ArcNode; temp->to = u; temp->r = r; temp->c = c; temp->next = NULL; if (list[v] == NULL) { list[v] = temp; } else { temp->next = list[v]; list[v] = temp; } } if (spfa(S)) { printf("YES\n"); } else { printf("NO\n"); } for (i = 1; i <= N; i++) { temp = list[i]; while (temp != NULL) { list[i] = temp->next; delete temp; temp = list[i]; } } } return 0;}
POJ 1860 Currency Exchange
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。