LemonSky95:代码写得比较丑。。求轻喷 #include <bits/stdc++.h>
using namespace std;
int cap[210];
int rd[210];
vector<int> toporder;
vector<int> G[210];
double tim[210];
double power[210];
bool vis[210];
void dfs(int cur)
{
vis[cur] = true;
tim[cur] = 0.0;
for (auto to : G[cur])
if (!vis[to])
dfs(to);
}
void topsort(int n)
{
toporder.clear();
queue<int> q;
for (int i = 1; i <= n; i++)
if (rd[i] == 0)
q.push(i);
while (!q.empty())
{
int cur = q.front();q.pop();
toporder.push_back(cur);
for (auto to : G[cur])
{
rd[to]--;
if (rd[to] == 0)
q.push(to);
}
}
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, m, b, e;
scanf("%d%d%d%d", &n, &m, &b, &e);
for (int i = 1; i <= n; i++)
scanf("%d", cap + i), rd[i] = 0, vis[i] = false, G[i].clear(), tim[i] = -1.0;
int from, to;
for (int i = 0; i < m; i++)
{
scanf("%d%d", &from, &to);
G[from].push_back(to);
rd[to]++;
}
if (e > 0)
{
dfs(b);
topsort(n);
for (int i = 1; i <= n; i++)
{
if (tim[i] == -1.0)
continue;
double l = 0.0, r = 65536.0*65536.0*200.0;
for (int j = 0; j < 50; j++)
{
double mid = (l + r) / 2;
for (int l = 1; l <= n; l++)
power[l] = 0;
power[b] = e * mid;
int k = 0;
while (toporder[k] != i)
{
int cur = toporder[k];
if (power[cur] >= cap[cur])
for (auto to : G[cur])
power[to] += (power[cur] - cap[cur]) / G[cur].size();
k++;
}
if (power[i] >= cap[i])
r = mid;
else
l = mid;
}
tim[i] = (l + r) / 2.0;
}
}
printf("%.4f", tim[1]);
for (int i = 2; i <= n; i++)
printf(" %.4f", tim[i]);
puts("");
}
return 0;
}
投递网易等公司10个岗位 >
0 点赞 评论 收藏
分享
关注他的用户也关注了: