forked from sanghaisubham/Algorithmic-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack(Classical).cpp
More file actions
72 lines (60 loc) · 1.17 KB
/
Knapsack(Classical).cpp
File metadata and controls
72 lines (60 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,t;
cin>>n>>t;
while(n!=0 || t!=0)
{
int time[n+1][n+1],toll[n+1][n+1];
int dp[1005][55];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>time[i][j];
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>toll[i][j];
}
}
memset(dp,500000,sizeof(dp));
for(int i=0;i<=t;i++)
dp[i][0]=0;
for(int i=0;i<=t;i++)
{
for(int k=0;k<n;k++)
{
for(int j=0;j<n;j++)
{
if(i-time[k][j]>=0)
dp[i][j]=min(dp[i][j],toll[k][j]+dp[i-time[k][j]][k]);
}
}
}
int times=0;
if(dp[t][n-1]>=500000)
cout<<-1<<"\n";
else
{
for(int i=t;i>=1;i--)
{
if(dp[i][n-1]==dp[t][n-1])
{
continue;
}
else if(dp[i][n-1]!=dp[t][n-1])
{
times=i+1;
break;
}
}
cout<<dp[t][n-1]<<" "<<times<<endl;
}
cin>>n>>t;
}
}