Skip to content

Commit db1b897

Browse files
committed
Added C++ solutions of (SPOJ)MISERMAN, (SPOJ)MPILOT and (SPOJ)PARTY
1 parent 48c1780 commit db1b897

File tree

3 files changed

+256
-0
lines changed

3 files changed

+256
-0
lines changed
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//Author: rishab_1128
2+
3+
#include<bits/stdc++.h>
4+
using namespace std;
5+
6+
#define Min(x,y,z) min(x,min(y,z))
7+
8+
void solve()
9+
{
10+
int n,m;
11+
cin>>n>>m;
12+
13+
int**fares=new int*[n];
14+
for(int i=0; i<n; i++)
15+
{
16+
fares[i]=new int[m];
17+
for(int j=0; j<m; j++)
18+
cin>>fares[i][j];
19+
}
20+
21+
int**dp=new int*[n];
22+
for(int i=0; i<n; i++)
23+
{
24+
dp[i]=new int[m];
25+
}
26+
27+
//Initialising the 1st row of the DP
28+
for(int j=0; j<m; j++)
29+
{
30+
dp[0][j]=fares[0][j];
31+
}
32+
33+
for(int i=1; i<n; i++)
34+
{
35+
for(int j=0; j<m; j++)
36+
{
37+
if(j==0)
38+
dp[i][j]=min(dp[i-1][j],dp[i-1][j+1])+fares[i][j];
39+
else if(j==m-1)
40+
dp[i][j]=min(dp[i-1][j],dp[i-1][j-1])+fares[i][j];
41+
else
42+
dp[i][j]=Min(dp[i-1][j],dp[i-1][j+1],dp[i-1][j-1])+fares[i][j];
43+
}
44+
}
45+
46+
int ans=INT_MAX;
47+
for(int j=0; j<m; j++)
48+
{
49+
ans=min(ans,dp[n-1][j]);
50+
}
51+
52+
//Deleting the fares and DP array
53+
for(int i=0; i<n; i++)
54+
{
55+
delete[]fares[i];
56+
delete[]dp[i];
57+
}
58+
delete[]fares;
59+
delete[]dp;
60+
61+
cout<<ans<<endl;
62+
63+
}
64+
65+
int main()
66+
{
67+
std::ios::sync_with_stdio(false);
68+
69+
#ifndef ONLINE_JUDGE
70+
freopen("input.txt","r",stdin);
71+
freopen("output.txt","w",stdout);
72+
#endif
73+
74+
int t=1;
75+
//cin>>t;
76+
while(t--)
77+
{
78+
solve();
79+
}
80+
return 0;
81+
}

Dynamic Programming/(SPOJ)MPILOT.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//Author: rishab_1128
2+
3+
#include<bits/stdc++.h>
4+
using namespace std;
5+
6+
int dp[10005][5005];
7+
8+
int func(int n, int*cap, int*asst, int diff)
9+
{
10+
if(dp[n][diff]!=-1)
11+
return dp[n][diff];
12+
13+
if(n==0)
14+
return 0;
15+
16+
if(diff==0)
17+
{
18+
//If diff=0 then this ele. has to become an asst. for the upcoming captains
19+
return dp[n][diff]=asst[0]+func(n-1,cap+1,asst+1,1);
20+
}
21+
if(diff==n)
22+
{
23+
//If diff=n then this ele. has to become a captain. for the extra assistants
24+
return dp[n][diff]=cap[0]+func(n-1,cap+1,asst+1,diff-1);
25+
}
26+
else
27+
{
28+
int op1=asst[0]+func(n-1,cap+1,asst+1,diff+1);
29+
int op2=cap[0]+func(n-1,cap+1,asst+1,diff-1);
30+
return dp[n][diff]=min(op1,op2);
31+
}
32+
33+
}
34+
35+
void solve()
36+
{
37+
int n;
38+
cin>>n;
39+
40+
int*cap=new int[n];
41+
int*asst=new int[n];
42+
43+
for(int i=0; i<n; i++)
44+
{
45+
cin>>cap[i]>>asst[i];
46+
}
47+
48+
memset(dp,-1,sizeof(dp));
49+
50+
int ans = func(n,cap,asst,0);
51+
cout << ans;
52+
53+
}
54+
55+
int main()
56+
{
57+
std::ios::sync_with_stdio(false);
58+
59+
#ifndef ONLINE_JUDGE
60+
freopen("input.txt","r",stdin);
61+
freopen("output.txt","w",stdout);
62+
#endif
63+
64+
int t=1;
65+
//cin>>t;
66+
while(t--)
67+
{
68+
solve();
69+
}
70+
return 0;
71+
}

Dynamic Programming/(SPOJ)PARTY.cpp

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//Author: rishab1128
2+
3+
#include<bits/stdc++.h>
4+
using namespace std;
5+
6+
#define FF first
7+
#define SS second
8+
#define PB push_back
9+
#define PF push_front
10+
#define MP make_pair
11+
12+
pair<int,int> knapsack2(int* weights, int* values, int n, int maxWeight)
13+
{
14+
int DP[n+1][maxWeight+1];
15+
16+
//Filling 0th row and 0th col
17+
for(int w=0; w<=maxWeight; w++)
18+
{
19+
DP[0][w]=0;
20+
}
21+
for(int i=0; i<=n; i++)
22+
{
23+
DP[i][0]=0;
24+
}
25+
26+
int Weights[n+1],Values[n+1],j=1,k=1;
27+
for(int i=0; i<n; i++)
28+
{
29+
Weights[j]=weights[i];
30+
j++;
31+
}
32+
33+
for(int i=0; i<n; i++)
34+
{
35+
Values[k]=values[i];
36+
k++;
37+
}
38+
39+
for(int i=1; i<=n; i++)
40+
{
41+
for(int w=1; w<=maxWeight; w++)
42+
{
43+
if(Weights[i]<=w)
44+
DP[i][w]=max(DP[i-1][w],DP[i-1][w-Weights[i]]+Values[i]);
45+
else
46+
DP[i][w]=DP[i-1][w];
47+
}
48+
}
49+
50+
int maxvalue=DP[n][maxWeight];
51+
int minweight=0;
52+
for(int i=0;i<=maxWeight;i++)
53+
{
54+
if(DP[n][i]==maxvalue)
55+
{
56+
minweight=i;
57+
break;
58+
}
59+
}
60+
61+
return {minweight,maxvalue};
62+
}
63+
64+
void solve()
65+
{
66+
while(1)
67+
{
68+
int budget,n;
69+
cin>>budget>>n;
70+
71+
if(budget==0&&n==0)
72+
return;
73+
74+
int*fee=new int[n];
75+
int*fun=new int[n];
76+
77+
for(int i=0; i<n; i++)
78+
{
79+
cin>>fee[i]>>fun[i];
80+
}
81+
82+
cout<<knapsack2(fee,fun,n,budget).FF<<" "<<knapsack2(fee,fun,n,budget).SS<<endl;
83+
84+
}
85+
86+
}
87+
88+
int main()
89+
{
90+
std::ios::sync_with_stdio(false);
91+
92+
#ifndef ONLINE_JUDGE
93+
freopen("input.txt","r",stdin);
94+
freopen("output.txt","w",stdout);
95+
#endif
96+
97+
int t=1;
98+
//cin>>t;
99+
while(t--)
100+
{
101+
solve();
102+
}
103+
return 0;
104+
}

0 commit comments

Comments
 (0)