题意:N种货币,M条命令,现有货币种类s,数量V,接下来每条命令包含a, b货币 、a到b的汇率、a到b的税、b到a的汇率、b到a的税,换算公式为(数量 - 税)* 汇率 问交换后最后s的数量会不会增加。
spfa变形 最长路 或 判环 松弛过程改变
链接:poj 1860
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1005;
const int maxm = 100500;
double V;
int n, m, s, t; //n为点数 s为源点
int head[maxn]; //head[from]表示以head为出发点的邻接表表头在数组es中的位置,开始时所有元素初始化为-1
double d[maxn]; //储存到源节点的距离,在Spfa()中初始化
int cnt[maxn];
bool inq[maxn]; //这里inq作inqueue解释会更好,出于习惯使用了inq来命名,在Spfa()中初始化
int nodep; //在邻接表和指向表头的head数组中定位用的记录指针,开始时初始化为0
int pre[maxn];
struct node {
int v, next;
double hi, si;
}es[maxm];
void init() {
for(int i = 1; i <= n; i++) {
d[i] = -inf;
inq[i] = false;
cnt[i] = 0;
head[i] = -1;
pre[i] = -1;
}
nodep = 0;
}
void addedge(int from, int to, double weight, double w2)
{
es[nodep].v = to;
es[nodep].hi = weight;
es[nodep].si = w2;
es[nodep].next = head[from];
head[from] = nodep++;
}
bool spfa()
{
queue<int> que;
d[s] = V; //s为源点
inq[s] = 1;
que.push(s);
while(!que.empty()) {
int u = que.front();
que.pop();
inq[u] = false; //从queue中退出
//遍历邻接表
for(int i = head[u]; i != -1; i = es[i].next) { //在es中,相同from出发指向的顶点为从head[from]开始的一项,逐项使用next寻找下去,直到找到第一个被输
//入的项,其next值为-1
int v = es[i].v;
if(d[v] == -inf || d[v] < (d[u] - es[i].si) * es[i].hi) { //松弛(RELAX)操作
d[v] = (d[u] - es[i].si) * es[i].hi;
pre[v] = u;
if(!inq[v]) { //若被搜索到的节点不在队列que中,则把to加入到队列中去
inq[v] = true;
que.push(v);
if(++cnt[v] > n) {
return true;
}
}
}
}
}
if(d[s] > V) return true;
return false;
}
void putpath() {
stack<int> path;
int now = t;
while(1) {
path.push(now);
if(now == s) {
break;
}
now = pre[now];
}
while(!path.empty()) {
now = path.top();
path.pop();
printf("%d\n", now);
}
}
int main()
{
int T, kcase = 0;
while(cin >> n >> m >> s >> V) {
init();
int a, b;
double a1, a2, b1, b2;
while(m--) {
cin>> a >> b >> a1 >> a2 >> b1 >> b2;
addedge(a, b, a1, a2);
addedge(b, a, b1, b2);
}
if(spfa()) {
puts("YES");
}
else {
puts("NO");
}
}
return 0;
}
poj 2240 类似题 货币兑换
没写case wa了一发。。。。。。
链接:poj 2240
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1005;
const int maxm = 1000500;
int n, m, s, t; //n为点数 s为源点
int head[maxn]; //head[from]表示以head为出发点的邻接表表头在数组es中的位置,开始时所有元素初始化为-1
double d[maxn]; //储存到源节点的距离,在Spfa()中初始化
int cnt[maxn];
bool inq[maxn]; //这里inq作inqueue解释会更好,出于习惯使用了inq来命名,在Spfa()中初始化
int nodep; //在邻接表和指向表头的head数组中定位用的记录指针,开始时初始化为0
int pre[maxn];
struct node {
int v, next;
double w;
}es[maxm];
void init() {
for(int i = 1; i <= n; i++) {
d[i] = -inf;
inq[i] = false;
cnt[i] = 0;
head[i] = -1;
pre[i] = -1;
}
nodep = 0;
}
void addedge(int from, int to, double weight)
{
es[nodep].v = to;
es[nodep].w = weight;
es[nodep].next = head[from];
head[from] = nodep++;
}
bool spfa()
{
queue<int> que;
d[s] = 1.0; //s为源点
inq[s] = 1;
que.push(s);
while(!que.empty()) {
int u = que.front();
que.pop();
inq[u] = false; //从queue中退出
//遍历邻接表
for(int i = head[u]; i != -1; i = es[i].next) { //在es中,相同from出发指向的顶点为从head[from]开始的一项,逐项使用next寻找下去,直到找到第一个被输
//入的项,其next值为-1
int v = es[i].v;
if(d[v] == -inf || d[v] < d[u] * es[i].w) { //松弛(RELAX)操作
d[v] = d[u] * es[i].w;
//pre[v] = u;
if(!inq[v]) { //若被搜索到的节点不在队列que中,则把to加入到队列中去
inq[v] = true;
que.push(v);
if(++cnt[v] > n) {
return false;
}
}
}
}
}
if(d[s] > 1) {
return false;
}
return true;
}
void putpath() {
stack<int> path;
int now = t;
while(1) {
path.push(now);
if(now == s) {
break;
}
now = pre[now];
}
while(!path.empty()) {
now = path.top();
path.pop();
printf("%d\n", now);
}
}
map<string, int > mp;
int main()
{
int T, kcase = 0;
while(cin >> n && n) {
init();
string str;
for(int i = 1; i <= n; i++) {
cin >> str;
mp[str] = i;
}
cin >> m;
while(m--) {
string a, b;
double h;
cin >> a >> h >> b;
addedge(mp[a], mp[b], h);
}
printf("Case %d: ", ++kcase);
if(!spfa()) {
puts("Yes");
}
else {
puts("No");
}
}
return 0;
}