题意:一个有向无环图( D A G DAG DAG 图),每条边有三种权重,经费负责人根据其中两种权重,按最小花费给处女座经费,处女座拿着经费按两种权重,走最小花费的路径,问处女座的经费的消耗情况
思路:由于存在负权边,所以没法用 d j dj dj 来求最短路,但是由于是 D A G DAG DAG 图,所以可以按拓扑序来求解最小花费,然后判断两种花费情况即可。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<deque>
#include<climits>
using namespace std;
#define ll long long
#define PI acos(-1)
#define INF 0x3f3f3f3f
#define NUM 100005
#define debug true
#define lowbit(x) ((-x)&x)
#define ffor(i,d,u) for(int i=(d);i<=(u);++i)
#define _ffor(i,u,d) for(int i=(u);i>=(d);--i)
#define mst(array,Num,Kind,Count) memset(array,Num,sizeof(Kind)*(Count))
const ll mod = 9999973;
template <typename T>
void read(T& x)
{
x=0;
char c;T t=1;
while(((c=getchar())<'0'||c>'9')&&c!='-');
if(c=='-'){t=-1;c=getchar();}
do(x*=10)+=(c-'0');while((c=getchar())>='0'&&c<='9');
x*=t;
}
template <typename T>
void write(T x)
{
int len=0;char c[21];
if(x<0)putchar('-'),x*=(-1);
do{++len;c[len]=(x%10)+'0';}while(x/=10);
_ffor(i,len,1)putchar(c[i]);
}
namespace Solve
{
int T, n, m;
int head[NUM], edgenum;
struct edge
{
int to, next;
ll w1, w2;
} e[NUM << 1];
ll dist1[NUM], dist2[NUM];
int du[NUM];
bool flag[NUM];
inline void BFS()//按拓扑序记忆化搜索
{
int x, y;
queue<int> q;
q.push(1);
dist1[1] = dist2[1] = 0;
while (!q.empty())
{
x = q.front(), q.pop();
for (int i = head[x]; i != 0; i = e[i].next)
{
y = e[i].to;
--du[y];
dist1[y] = min(dist1[y], dist1[x] + e[i].w1);
dist2[y] = min(dist2[y], dist2[x] + e[i].w2);
if (!du[y])
{
if (y == n)
return;
q.push(y);
}
}
}
}
inline void ToPo()//求从1为起点的所有路径上点的拓扑序
{
int x, y;
queue<int> q;
q.push(1);
flag[1] = true;
while (!q.empty())
{
x = q.front(), q.pop();
for (int i = head[x]; i != 0; i = e[i].next)
{
y = e[i].to;
if (flag[y] == false)
flag[y] = true, q.push(y);
}
}
ffor(i, 1, n)
{
if (!flag[i])
continue;
for (int j = head[i]; j != 0; j = e[j].next)
{
if (!flag[e[j].to])
continue;
++du[e[j].to];
}
}
}
inline void AC()
{
ll x, y, z;
read(T);
while(T--)
{
mst(flag, false, bool, n + 1), mst(du, 0, int, n + 1), mst(head, 0, int, n + 1);
read(n), read(m);
edgenum = 0;
ffor(i, 1, n)
dist1[i] = dist2[i] = 99999999999999999;
ffor(i, 1, m)
{
read(x), read(y);
e[++edgenum].to = y, e[edgenum].next = head[x], head[x] = edgenum;
read(z), read(x), read(y);
e[edgenum].w1 = z - x, e[edgenum].w2 = z - y;
}
ToPo();
BFS();
if (dist1[n] < 0)
dist1[n] = 0;
if (dist2[n] < 0)
dist2[n] = 0;
if (dist1[n] > dist2[n])
{
puts("rip!!!");
write(dist1[n] - dist2[n]);
putchar('\n');
}
else
{
if (dist1[n] == dist2[n])
{
puts("oof!!!");
}
else
{
puts("cnznb!!!");
write(dist2[n] - dist1[n]);
putchar('\n');
}
}
}
}
}
int main()
{
Solve::AC();
return 0;
}