Click to open link
Test instructions: There are n individuals, and then x relationship and y relationship, the x relationship represents that the distance of these two people can not exceed c,y represents the distance of these two people is greater than or equal to C, if not meet all the output-1, if the position of 1 and n can be infinitely large output-2, otherwise the maximum distance to output two people
Idea: is to check the model of the constraint, x relationship according to the location of the building edge, Y is directly by the location of the line, that is, two initial direction is different, then there is a negative ring output-1, distance infinity output-2, otherwise is dis[n] value on the line, you can come out with a discussion
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <
Algorithm> using namespace std;
typedef long Long LL;
const int inf=0x3f3f3f3f;
const int maxn=20010;
struct edge{int from,to,cost;};
Edge ES[MAXN];
ll DIS[MAXN];
int v,e;
void Shortest_path (int s) {for (int i=0;i<=v;i++) Dis[i]=inf;
dis[s]=0;
while (1) {bool update=0;
for (int i=0;i<e;i++) {edge e=es[i];
if (dis[e.from]!=inf&&dis[e.to]>dis[e.from]+e.cost) {dis[e.to]=dis[e.from]+e.cost;
update=1;
}} if (!update) break;
}} bool Find_negative_loop () {memset (dis,0,sizeof (dis));
for (int i=0;i<v;i++) {for (int j=0;j<e;j++) {edge e=es[j];
if (dis[e.to]>dis[e.from]+e.cost) {dis[e.to]=dis[e.from]+e.cost;
if (I==v-1) return 1; }}} return0;
} int main () {int t,x,y,a,b,c;
scanf ("%d", &t);
while (t--) {scanf ("%d%d%d", &v,&x,&y);
E=x+y;
int k=0;
while (x--) {scanf ("%d%d%d", &a,&b,&c);
if (a<b) {es[k].from=a;es[k].to=b;es[k++].cost=c;}
else {es[k].from=a;es[k].to=b;es[k++].cost=-c;}
} while (y--) {scanf ("%d%d%d", &a,&b,&c);
if (b<a) {es[k].from=b;es[k].to=a;es[k++].cost=c;}
else {es[k].from=b;es[k].to=a;es[k++].cost=-c;}
} if (Find_negative_loop ()) {printf (" -1\n");
Continue
} shortest_path (1);
if (dis[v]==inf) printf (" -2\n");
else printf ("%d\n", Dis[v]);
} return 0;
}