首页 > 代码库 > 传送poj1275

传送poj1275

技术分享
 1 #include<cmath>
 2 #include<queue>
 3 #include<cstdio>
 4 #include<vector>
 5 #include<cstdlib>
 6 #include<cstring>
 7 #include<iostream>
 8 #include<algorithm>
 9 #define N 1010
10 #define RG register
11 #define inf 0x3f3f3f3f
12 #define Inf 99999999999999999LL
13 using namespace std;
14 typedef long long LL;
15 struct node{
16     int to,val;
17 }e[N*N];
18 int l,r,n,T,ti,ans,cnt,R[25],t[25],dis[N],head[N];
19 inline int Abs(RG const int &a){return a>0?a:-a;}
20 inline int Max(RG const int &a,RG const int &b){return a>b?a:b;}
21 inline int Min(RG const int &a,RG const int &b){return a>b?b:a;}
22 inline int gi(){
23     RG int x=0;RG bool flag=0;RG char c=getchar();
24     while((c<0||c>9)&&c!=-) c=getchar();
25     if(c==-) c=getchar(),flag=1;
26     while(c>=0&&c<=9) x=x*10+c-0,c=getchar();
27     return flag?-x:x;
28 }
29 inline void init(){
30     ans=inf;
31     memset(t,0,sizeof(t));
32     for (RG int i=1;i<=24;++i)
33     R[i]=gi();
34     n=gi();l=1,r=n;
35     for (RG int i=1;i<=n;++i){
36     ti=gi();
37     ++t[ti+1];
38     }
39 }
40 inline void addedge(RG int from,RG int to,RG int w){
41     e[++cnt].to=to;
42     e[cnt].val=w;
43     e[cnt].next=head[from];
44     head[from]=cnt;
45 }
46 inline void SPFA(){
47     
48 
49 
50 
51 }
52 inline bool check(RG int sum){
53     memset(e,0,sizeof(e));
54     memset(head,0,sizeof(head));
55     memset(dis,-inf,sizeof(dis));
56     cnt=0;dis[0]=0;
57     for (RG int i=1;i<=24;++i)
58     addedge(i,i-1,0);
59     for (RG int i=1;i<=24;++i)
60     addedge(i-1,i,-t[i]);
61     addedge(0,24,sum);
62     for (RG int i=0;i<24;++i){
63     RG int j=(i+8)%24;
64     if(j>i) addedge(i+1,j+1,r[j+1]);
65     else    addedge(i+1,i+1,r[j+1]-sum);
66     }
67     SPFA();
68 }
69 int main(){
70     freopen("1275.in","r",stdin);
71     freopen("1275.out","w",stdout);
72     T=gi();
73     while(T--){
74     init();
75     while(l<=r){
76         RG int mid=(l+r)>>1;   
77         if(check(mid)) ans=mid,r=mid-1;
78         else           l=mid+1;
79     }
80     if(ans==inf) printf("No Solution\n");
81     else         printf("%d\n",ans);
82     }
83     fclose(stdin);
84     fclose(stdout);
85     return 0;
86 }
View Code

 

传送poj1275