1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; template<typename T>inline void read(T &x) { int ch=getchar(); T f=1; x=0; while(!('0'<=ch&&ch<='9')) { if(ch=='-') f=-1; ch=getchar(); } while('0'<=ch&&ch<='9') { x=(x<<3)+(x<<1)+ch-48; ch=getchar(); } x*=f; } const int N=1e4+10; int n,m; int head[N],to[(N*10)<<1],nxt[(N*10)<<1],w[(N*10)<<1],e; int ans[N],cnt[N]; bool vis[N]; queue<int>q; inline void add(const int &u,const int &v,const int &c) { to[++e]=v; w[e]=c; nxt[e]=head[u]; head[u]=e; } inline bool spfa(const int &s) { while(!q.empty()) q.pop(); memset(ans,0x3f,sizeof(ans)); memset(vis,0,sizeof(vis)); memset(cnt,0,sizeof(cnt)); ans[s]=0; vis[s]=1; q.push(s); while(!q.empty()) { int x=q.front(); q.pop(); vis[x]=0; for(int i=head[x];i;i=nxt[i]) { int y=to[i]; if(ans[y]>ans[x]+w[i]) { ans[y]=ans[x]+w[i]; cnt[y]=cnt[x]+1; if(cnt[y]>n) return 1; if(!vis[y]) { vis[y]=1; q.push(y); } } } } return 0; } int main() { read(n); read(m); int u,v,w; for(int i=1;i<=m;++i) { read(u); read(v); read(w); add(u,v,-w); } if(spfa(1)) { printf("Forever love"); return 0; } int res=ans[n]; if(spfa(n)) printf("Forever love"); else printf("%d",min(res,ans[1])); return 0; }
|