P1938 [USACO09NOV]找工就业Job Hunt
给边赋予价值,入边的权值为D-Ti,然后从起点开始跑最长路,如果钱的总数超过了D*C,也就是一定有一个城市走了两遍,则有正环,则输出-1

 #include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define ls rt<<1
#define rs rt<<1|1
#define lson ls,nl,mid,l,r
#define rson rs,mid+1,nr,l,r
#define N 100010
#define For(i,a,b) for(int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar() using namespace std;
int D,P,C,F,S;
int b[][];
int x,y,v;
int vis[];
int ans;
int d[]; struct node{
int n;
int v;
node *next;
}*e[]; queue<int>q; void in(int &x){
int y=;
char c=g();x=;
while(c<''||c>''){
if(c=='-')y=-;
c=g();
}
while(c<=''&&c>=''){
x=(x<<)+(x<<)+c-'';c=g();
}
x*=y;
}
void o(int x){
if(x<){
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} void push(int x,int y,int v){
node *p;
p=new node();
p->n=y;
p->v=v;
if(e[x]==NULL)
e[x]=p;
else{
p->next=e[x]->next;
e[x]->next=p;
}
} void hd(){
d[S]=D;
q.push(S);
while(!q.empty()){
int t=q.front();
q.pop();
vis[t]=true;
for(node *i=e[t];i;i=i->next){
if(d[t]+i->v>d[i->n]){
d[i->n]=d[t]+i->v;
if(!vis[i->n])
q.push(i->n);
}
}
vis[t]=false;
}
} int main(){
in(D);in(P);in(C);in(F);in(S);
For(i,,P){
in(x);in(y);
b[x][y]=;
}
For(i,,F){
in(x);in(y);in(v);
push(x,y,D-v);
if(D-v!=)
b[x][y]=D-v;
else
b[x][y]=;
}
For(i,,C)
For(j,,C)
if(b[i][j]==)
push(i,j,D);
For(i,,C)
d[i]=-inf;
hd();
ans=-inf;
For(i,,C)
ans=max(ans,d[i]);
if(ans>C*D)
o(-);
else
o(ans);
return ;
}
05-06 05:33