题目链接 : https://ac.nowcoder.com/acm/contest/329/B
拓扑排序,主要注意下,一条路走下来经费负责人和处女座一条路下来如果有赚都要置0,我看比赛过了的代码有些好像没有置0 ,可能是比赛结束之后数据加强了
#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
struct consu
{
long long int chunv, jinfei;
};
consu cost[100005];
struct node
{
long long int v,cnz,jffzr;
};
vector<node>top[100005];
int num[100005], n, m;
void toop()
{
cost[1].chunv = 0;
cost[1].jinfei = 0;
int i, j;
queue<int>q;
for(i = 1; i <= n; i++)
if(num[i]==0)
q.push(i);
while(!q.empty())
{
int r = q.front();
q.pop();
for(j = 0; j < top[r].size(); j++)
{
if(cost[top[r][j].v].chunv > cost[r].chunv+top[r][j].cnz)
cost[top[r][j].v].chunv = cost[r].chunv+top[r][j].cnz;
if(cost[top[r][j].v].jinfei > cost[r].jinfei+top[r][j].jffzr)
cost[top[r][j].v].jinfei = cost[r].jinfei+top[r][j].jffzr;
num[top[r][j].v]--;
if(num[top[r][j].v]==0)
q.push(top[r][j].v);
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
int t, c, cnz, jffzr, i, u, v;
cin >> t;
while(t--)
{
cin >> n >> m;
for(i = 0; i <= n; i++)
top[i].clear();
memset(num, 0, sizeof(num));
memset(cost, INF, sizeof(cost));
for(i = 1; i <= m; i++)
{
node temp;
cin >> u >> v >> c >> cnz >> jffzr;
temp.v = v;
temp.cnz = c-cnz;
temp.jffzr = c-jffzr;
top[u].push_back(temp);
}
toop();
if(cost[n].jinfei<=0) cost[n].jinfei = 0;
if(cost[n].chunv<=0) cost[n].chunv = 0;
if(cost[n].jinfei > cost[n].chunv)
{
cout << "cnznb!!!" << endl;
cout << (cost[n].jinfei - cost[n].chunv) << endl;
}
else if(cost[n].jinfei == cost[n].chunv)
{
cout << "oof!!!" << endl;
}
else
{
cout << "rip!!!" << endl;
cout << -(cost[n].jinfei - cost[n].chunv) << endl;
}
}
return 0;
}