如果nick能够实现他的愿望,则输出YES,否则输出NO。
思路:判断图中有没有正权环。BellmamFord修改下即可。A到B的权值为(A现有的钱 - 佣金)*汇率,即(money - cost)*rate。初始化,源点s的钱为V,其他均为极小值。松弛条件改为权值可以变大时,进行松弛。
队列优化BellmanFord(SPFA):
//#include
#include
#include
#include
#include
#include
using namespace std;
const int MAXN = 1e3 + 5;
const int INF = 0x3f3f3f3f;
struct Edge
{
int from, to/*, dist*/; //起点,终点,距离
double cost, rate;
//Edge(int u, int v, int w):from(u), to(v), dist(w) {}
Edge(int u, int v, double r, double c):from(u), to(v), rate(r), cost(c) {}
};
struct BellmanFord
{
int n, m; //结点数,边数(包括反向弧)
vector edges; //边表。edges[e]和edges[e^1]互为反向弧
vector G[MAXN]; //邻接表,G[i][j]表示结点i的第j条边在edges数组中的序号
int vis[MAXN]; //标记数组
//int d[MAXN]; //s到各个点的最短路
double d[MAXN];
int cnt[MAXN]; //入队次数
int pre[MAXN]; //上一条弧
void init(int n)
{
this->n = n;
edges.clear();
for (int i = 0; i <= n; i++) G[i].clear();
}
//void add_edge(int from, int to, int dist)
void add_edge(int from, int to, double rate, double cost)
{
edges.push_back(Edge(from, to, rate, cost));
m = edges.size();
G[from].push_back(m - 1);
}
bool bellman_ford(int s, double v)
{
for (int i = 0; i <= n; i++) d[i] = -INF;
d[s] = v; vis[s] = true;
memset(vis, 0, sizeof(vis));
memset(cnt, 0, sizeof(cnt));
queue Q;
Q.push(s);
while (!Q.empty())
{
int u = Q.front(); Q.pop();
vis[u] = false;
for (int i = 0; i < G[u].size(); i++)
{
Edge& e = edges[G[u][i]];
if (d[u] > -INF && d[e.to] < (d[e.from] - e.cost) * e.rate)
{
d[e.to] = (d[e.from] - e.cost) * e.rate;
pre[e.to] = G[u][i];
if (!vis[e.to])
{
Q.push(e.to); vis[e.to] = true;
if (++cnt[e.to] > n) return false;
}
}
}
}
return true;
}
};
BellmanFord solve;
int main()
{
int n, m, s;
double v;
while (~scanf("%d%d%d%lf", &n, &m, &s, &v))
{
solve.init(n);
while (m--)
{
int a, b;
double rate1, cost1, rate2, cost2;
scanf("%d%d%lf%lf%lf%lf", &a, &b, &rate1, &cost1, &rate2, &cost2);
solve.add_edge(a, b, rate1, cost1);
solve.add_edge(b, a, rate2, cost2);
}
if (!solve.bellman_ford(s, v)) printf("YES\n");
else printf("NO\n");
}
return 0;
}
/*
3 2 1 20.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00
*/
BellmanFord:
//#include
#include
#include
#include
#include
using namespace std;
const int MAXN = 1e3 + 5;
const int INF = 0x3f3f3f3f;
struct Edge
{
int from, to/*, dist*/; //起点,终点,距离
double cost, rate;
//Edge(int u, int v, int w):from(u), to(v), dist(w) {}
Edge(int u, int v, double r, double c):from(u), to(v), rate(r), cost(c) {}
};
struct BellmanFord
{
int n, m; //结点数,边数(包括反向弧)
vector edges; //边表。edges[e]和edges[e^1]互为反向弧
vector G[MAXN]; //邻接表,G[i][j]表示结点i的第j条边在edges数组中的序号
int vis[MAXN]; //标记数组
//int d[MAXN]; //s到各个点的最短路
double d[MAXN];
int pre[MAXN]; //上一条弧
void init(int n)
{
this->n = n;
edges.clear();
for (int i = 0; i <= n; i++) G[i].clear();
}
//void add_edge(int from, int to, int dist)
void add_edge(int from, int to, double rate, double cost)
{
edges.push_back(Edge(from, to, rate, cost));
m = edges.size();
G[from].push_back(m - 1);
}
bool bellman_ford(int s, double v)
{
for (int i = 0; i <= n; i++) d[i] = -INF;
d[s] = v;
for (int k = 0; k < n - 1; k++)
{
bool flag = false;
for (int i = 0; i < m; i++)
{
Edge& e = edges[i];
if (d[e.from] > -INF && d[e.to] < (d[e.from] - e.cost) * e.rate)
{
flag = true;
d[e.to] = (d[e.from] - e.cost) * e.rate;
pre[e.to] = i;
}
}
if (!flag) break;
}
for (int i = 0; i < m; i++)
{
Edge& e = edges[i];
if (d[e.from] > -INF && d[e.to] < (d[e.from] - e.cost) * e.rate) return false;
}
return true;
}
};
BellmanFord solve;
int main()
{
int n, m, s;
double v;
while (~scanf("%d%d%d%lf", &n, &m, &s, &v))
{
solve.init(n);
while (m--)
{
int a, b;
double rate1, cost1, rate2, cost2;
scanf("%d%d%lf%lf%lf%lf", &a, &b, &rate1, &cost1, &rate2, &cost2);
solve.add_edge(a, b, rate1, cost1);
solve.add_edge(b, a, rate2, cost2);
}
if (!solve.bellman_ford(s, v)) printf("YES\n");
else printf("NO\n");
}
return 0;
}
/*
3 2 1 20.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00
*/