1. 程式人生 > >Day 10.3

Day 10.3

pri -a style emp double std log make air

T1

完全背包

 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 int n,m,p,q,a,b,c;
 5 double d,ans=0,f[51][51];
 6 int main()
 7 {
 8     freopen("equipment.in","r",stdin);
 9     freopen("equipment.out","w",stdout);
10     scanf("%d%d%d%d",&n,&m,&p,&q);
11     for
(int i=1;i<=p;i++) 12 { 13 scanf("%d%d%d",&a,&b,&c); 14 for (int j=0;j<=n-a;j++) 15 for (int k=0;k<=m-b;k++) 16 f[j+a][k+b]=max(f[j+a][k+b],f[j][k]+c); 17 } 18 for (int i=1;i<=q;i++) 19 { 20 scanf("%d%d%lf",&a,&b,&d);
21 for (int j=0;j<=n-a;j++) 22 for (int k=0;k<=m-b;k++) 23 f[j+a][k+b]=max(f[j+a][k+b],f[j][k]*d); 24 } 25 for (int j=0;j<=n;j++) 26 for (int k=0;k<=m;k++) 27 ans=max(ans,f[j][k]); 28 printf("%.2lf",ans); 29 }

T2

矩陣快速冪

 1 #include <cstdio>
 2
#include <cstring> 3 using namespace std; 4 struct Mat{ 5 double s[101][101]; 6 }mat0,mat1,matz; 7 int n,m,x,y; 8 Mat matmul(Mat a,Mat b) 9 { 10 Mat s; 11 for (int i=1;i<=n;i++) 12 for (int j=1;j<=n;j++) 13 { 14 s.s[i][j]=0; 15 for (int k=1;k<=n;k++) 16 s.s[i][j]+=a.s[i][k]*b.s[k][j]; 17 } 18 return s; 19 } 20 Mat matpow(Mat a,int b) 21 { 22 Mat s=matz; 23 while(b) 24 { 25 if (b%2==1) s=matmul(s,a); 26 a=matmul(a,a); 27 b/=2; 28 } 29 return s; 30 } 31 int main() 32 { 33 freopen("cocktail.in","r",stdin); 34 freopen("cocktail.out","w",stdout); 35 memset(mat0.s,0,sizeof(mat0.s)); 36 memset(mat1.s,0,sizeof(mat1.s)); 37 memset(matz.s,0,sizeof(matz.s)); 38 scanf("%d%d",&n,&m); 39 for (int i=1;i<=n;i++) matz.s[i][i]=1; 40 for (int i=1;i<=n;i++) 41 { 42 scanf("%d",&x); 43 mat1.s[1][i]=x; 44 } 45 for (int i=1;i<=n;i++) 46 { 47 scanf("%d",&x); 48 mat0.s[i][i]=(double)1/(x+1); 49 for (int j=1;j<=x;j++) 50 { 51 scanf("%d",&y); 52 mat0.s[i][y]=(double)1/(x+1); 53 } 54 } 55 mat0=matpow(mat0,m); 56 mat0=matmul(mat1,mat0); 57 for (int i=1;i<=n;i++) 58 printf("%.4lf\n",mat0.s[1][i]); 59 }

T3

雙向BFS

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <map>
 5 #include <queue>
 6 #include <cstdlib>
 7 using namespace std;
 8 struct Q{
 9     string s;
10     int x,y;
11 };
12 map<string,int> m1;
13 map<string,int> m2;
14 queue<Q> que;
15 int t,ak,d[9]={1,2,-1,-2,1,-2,-1,2,1};
16 string read()
17 {
18     string s;char c;
19     for (int i=0;i<5;i++)
20     {
21         getchar();
22         for (int j=0;j<5;j++)
23         {
24             scanf("%c",&c);
25             s+=c;
26         }
27     }
28     return s;
29 }
30 int getsp(string s)
31 {
32     for (int i=0;i<25;i++) if (s[i]==*) return i;
33 }
34 void bfs1(int k)
35 {
36     while(!que.empty())
37     {
38         Q x=que.front(),y;que.pop();
39         int a=x.x,b=x.y,sp=a+b*5,a2,b2,sp2;
40         for (int i=0;i<8;i++)
41         {
42             a2=a+d[i],b2=b+d[i+1],sp2=a2+b2*5;
43             if (a2>=0&&a2<5&&b2>=0&&b2<5)
44             {
45                 y.s=x.s,y.x=a2,y.y=b2;
46                 y.s[sp]=y.s[sp2];y.s[sp2]=*;
47                 if ((m1[x.s]<k)&&(!m1.count(y.s)||m1[x.s]+1<m1[y.s]))
48                 {
49                     m1.insert(make_pair(y.s,m1[x.s]+1));
50                     que.push(y);
51                 }
52             }
53         }
54     }
55 }
56 int bfs2(int k)
57 {
58     int ans=2000000000;
59     while(!que.empty())
60     {
61         Q x=que.front(),y;que.pop();
62         if (m1.count(x.s)&&m1[x.s]+m2[x.s]<ans) ans=min(ans,m1[x.s]+m2[x.s]);
63         int a=x.x,b=x.y,sp=a+b*5,a2,b2,sp2;
64         for (int i=0;i<8;i++)
65         {
66             a2=a+d[i],b2=b+d[i+1],sp2=a2+b2*5;
67             if (a2>=0&&a2<5&&b2>=0&&b2<5)
68             {
69                 y.s=x.s,y.x=a2,y.y=b2;
70                 y.s[sp]=y.s[sp2];y.s[sp2]=*;
71                 if ((m2[x.s]<k)&&(!m2.count(y.s)||m2[x.s]+1<m2[y.s]))
72                 {
73                     m2.insert(make_pair(y.s,m2[x.s]+1));
74                     que.push(y);
75                 }
76             }
77         }
78     }
79     if (ans>ak) return -1;
80     return ans;
81 }
82 int main()
83 {
84     freopen("knight.in","r",stdin);
85     freopen("knight.out","w",stdout);
86     string s;
87     scanf("%d%d",&t,&ak);
88     {s=read();Q q;int sp=getsp(s),a=sp%5,b=sp/5;q.s=s,q.x=a,q.y=b;m1.insert(make_pair(s,0));que.push(q);}
89     bfs1(9);
90     for (int i=1;i<=t;i++)
91     {
92         m2.clear();
93         {s=read();Q q;int sp=getsp(s),a=sp%5,b=sp/5;q.s=s,q.x=a,q.y=b;m2.insert(make_pair(s,0));que.push(q);}
94         printf("%d\n",bfs2(6));
95     }
96 }

Day 10.3