题意就是求S点到T点的有向无环图中的最长路。
用SPFA可以解决。
一开始一直RE的原因 QAQ
竟然是在开Edge 邻接表的时候开小了
改了一下4Y
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm> #define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
const int MAXSIZE = ; int dis[MAXSIZE],head[MAXSIZE],vis[MAXSIZE];
int n, cnt; struct node{
int u,v,val,next;
} Edge[MAXN]; void addEdge(int u,int v,int val){
Edge[cnt].u=u;
Edge[cnt].v=v;
Edge[cnt].val=val;
Edge[cnt].next=head[u];
head[u]=cnt++;
} void init(){
memset(head, -, sizeof(head));
memset(vis, , sizeof(vis));
for(int i = ; i <= n; ++i) dis[i] = -INF;
cnt = ;
} int spfa(int src, int ter){
queue<int>q;
q.push(src);
vis[src]=;
dis[src]=;
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=;
for(int i=head[u]; i!=-; i=Edge[i].next){
int v=Edge[i].v;
if(dis[v]<dis[u]+Edge[i].val){
dis[v]=dis[u]+Edge[i].val;
if(!vis[v]){
vis[v]=;
q.push(v);
}
}
}
}
return dis[ter];
} int main(){
int i, j, k, t, numCase;
int ss, ee, val;
int s, e;
while(EOF != scanf("%d%d",&n,&numCase)){
init();
while(numCase--){
scanf("%d%d%d",&ss,&ee,&val);
addEdge(ss,ee,val);
}
scanf("%d%d",&s,&e);
int ans = spfa(s, e);
if(ans > ) printf("%d\n",spfa(s, e));
else printf("No solution\n");
}
return ;
}