赚钱啦
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
5
-
描述
-
某国家里有N个城市,分别编号为0~N-1,一个精明的商人准备从0号城市旅行到N-1号城市,在旅行的过程中,从一个城市移动到另外一个城市需要有一定的花费,并且从A城市移动到B城市的花费和B城市移动到A城市的花费相同,但是,从A城市移动到B城市能赚取的钱和从B城市移动到A城市赚的钱不一定相同。
现在,已知各个城市之间移动的花费和城市之间交易可赚取的金钱,求该商人在从0号城市移动到N-1号城市的过程中最多能赚取多少钱?
-
输入
-
第一行是一个整数T(T<=10)表示测试数据的组数
每组测试数据的第一行是两个整数N,M表示,共有N个城市(1<N<=1000),M条路(1<=M<=1000)
随后的M行,每行有5个正整数,前两个数a,b(0<=a,b<N)表示两个城市的编号。后面的三个数c,u,v分别表示在a,b城市之间移动的花费,a城市移动到b城市可赚取的资金,b城市移动到a城市可赚取的资金。
(0<=c,u,v<=1000)
输出
-
如果商人能够在旅行过程中赚取无限多的资金,则输出$$$
否则输出他在移动过程中最多能赚取的资金数量
如果只会赔钱的话就输出一个负数,表示最少赔的钱数。
样例输入
-
2 2 1 0 1 10 11 11 3 3 0 1 10 16 0 1 2 10 15 5 0 2 20 32 0
样例输出
-
$$$ 12
-
第一行是一个整数T(T<=10)表示测试数据的组数
让权值为:花费-赚取 ,然后求最短路,如果有负圈就说明能无限赚钱 。对结果取反。
#include <stdio.h>
#include <queue>
using namespace std;
#define INF 99999999
#define Maxsize 1000
struct Edge{
int from,to,cost;
}edge[Maxsize][Maxsize];
int dist[Maxsize];
int Count[Maxsize];
bool inque[Maxsize];
void AddEdge(int from,int to,int cost)
{
int t=++edge[from][0].to;
edge[from][t].to=to;
edge[from][t].cost=cost;
}
void Init(int N)
{
for(int i=0;i<=N;i++)
{
edge[i][0].to=0;
Count[i]=0;
dist[i]=INF;
inque[i]=0;
}
}
void spfa(int N)
{
queue<int> que;
que.push(0);
dist[0]=0;
inque[0]=1;
int u;
while(!que.empty())
{
u=que.front();que.pop();
inque[u]=0;
Count[u]++;
if(Count[u]>N)
{
printf("$$$\n");
return;
}
for(int i=1;i<=edge[u][0].to;i++)
{
Edge &e=edge[u][i];
if(dist[e.to]>dist[u]+e.cost)
{
dist[e.to]=dist[u]+e.cost;
if(!inque[e.to])
{
que.push(e.to);
inque[e.to]=1;
}
}
}
}
printf("%d\n",(-1)*dist[N-1]);
}
int main()
{
int T;
int N,M;
int i;
int a,b,c,u,v;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&M);
Init(N);
for(i=0;i<M;i++)
{
scanf("%d%d%d%d%d",&a,&b,&c,&u,&v);
AddEdge(a,b,c-u);
AddEdge(b,a,c-v);
}
spfa(N);
}
return 0;
}