P1938 [USACO09NOV]找工就业Job Hunt
P1938 [USACO09NOV]找工就业Job Hunt
给边赋予价值,入边的权值为D-Ti,然后从起点开始跑最长路,如果钱的总数超过了D*C,也就是一定有一个城市走了两遍,则有正环,则输出-1
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 #include<cmath> 6 #include<ctime> 7 #include<set> 8 #include<map> 9 #include<stack> 10 #include<cstring> 11 #define inf 2147483647 12 #define ls rt<<1 13 #define rs rt<<1|1 14 #define lson ls,nl,mid,l,r 15 #define rson rs,mid+1,nr,l,r 16 #define N 100010 17 #define For(i,a,b) for(int i=a;i<=b;i++) 18 #define p(a) putchar(a) 19 #define g() getchar() 20 21 using namespace std; 22 int D,P,C,F,S; 23 int b[300][300]; 24 int x,y,v; 25 int vis[1010]; 26 int ans; 27 int d[1010]; 28 29 struct node{ 30 int n; 31 int v; 32 node *next; 33 }*e[1010]; 34 35 queue<int>q; 36 37 void in(int &x){ 38 int y=1; 39 char c=g();x=0; 40 while(c<'0'||c>'9'){ 41 if(c=='-')y=-1; 42 c=g(); 43 } 44 while(c<='9'&&c>='0'){ 45 x=(x<<1)+(x<<3)+c-'0';c=g(); 46 } 47 x*=y; 48 } 49 void o(int x){ 50 if(x<0){ 51 p('-'); 52 x=-x; 53 } 54 if(x>9)o(x/10); 55 p(x%10+'0'); 56 } 57 58 void push(int x,int y,int v){ 59 node *p; 60 p=new node(); 61 p->n=y; 62 p->v=v; 63 if(e[x]==NULL) 64 e[x]=p; 65 else{ 66 p->next=e[x]->next; 67 e[x]->next=p; 68 } 69 } 70 71 void hd(){ 72 d[S]=D; 73 q.push(S); 74 while(!q.empty()){ 75 int t=q.front(); 76 q.pop(); 77 vis[t]=true; 78 for(node *i=e[t];i;i=i->next){ 79 if(d[t]+i->v>d[i->n]){ 80 d[i->n]=d[t]+i->v; 81 if(!vis[i->n]) 82 q.push(i->n); 83 } 84 } 85 vis[t]=false; 86 } 87 } 88 89 int main(){ 90 in(D);in(P);in(C);in(F);in(S); 91 For(i,1,P){ 92 in(x);in(y); 93 b[x][y]=1; 94 } 95 For(i,1,F){ 96 in(x);in(y);in(v); 97 push(x,y,D-v); 98 if(D-v!=1) 99 b[x][y]=D-v; 100 else 101 b[x][y]=0; 102 } 103 For(i,1,C) 104 For(j,1,C) 105 if(b[i][j]==1) 106 push(i,j,D); 107 For(i,1,C) 108 d[i]=-inf; 109 hd(); 110 ans=-inf; 111 For(i,1,C) 112 ans=max(ans,d[i]); 113 if(ans>C*D) 114 o(-1); 115 else 116 o(ans); 117 return 0; 118 }