Professional Documents
Culture Documents
Output minimal-weight path, and the second line is the cost of a minimal path.
Input: Output:
56 123445
341286 16
618274
593995
841326
372864
Source Code
fix= infinity; for(j=m-1;j>0;j--)
for(i=0;i<n;i++)
for(i=0;i<n;i++)
{
for(j=0;j<m;j++) if(si[i][j-1] > si[i][j] + sa[i][j-1] || (si[i][j-1] ==
{ si[i][j] + sa[i][j-1] && t1[i][j-1] > i))
scanf("%lld",&sa[i][j]); {
si[i][j]=fix; si[i][j-1]=si[i][j]+sa[i][j-1];
t1[i][j-1]=i;
t1[i][j]=-1;
}
} k=i-1;
for(i=0;i<n;i++) if(k<0)
si[i][m-1]=sa[i][m-1]; k=k+n;
if(si[k][j-1] > si[i][j] + sa[k][j-1] || (si[k][j-1] ==
si[i][j] + sa[k][j-1] && t1[k][j-1] > i))
{
si[k][j-1]=si[i][j]+sa[k][j-1];
t1[k][j-1]=i;
}
Source Code
k=i+1; printf("%ld",k+1);
k=k%n;
if(si[k][j-1] > si[i][j] + sa[k][j-1] || j=0;
(si[k][j-1] == si[i][j] + sa[k][j-1]
&& t1[k][j-1] > i)) while(t1[k][j]!=-1)
{ {
printf(" %ld",t1[k][j]+1);
si[k][j-1]=si[i][j]+sa[k][j-1];
k=t1[k][j];
t1[k][j-1]=i; j++;
} }
}
min=fix; printf("\n%lld\n",min);
for(i=0;i<n;i++)
Sample Problems:
if(si[i][0]<min)
•UVA Online Judge: 116.
{
min=si[i][0];
k=i;
}
Coin change
sa[0]=1; while(scanf("%ld",&i)==1)
{
printf("%ld\n",sa[i]);
}
Coin change
• Sample Problems:
1.UVA Online Judge: 147, 166, 357, 674, 10306,
10313, 11137, 11517.
The longest common subsequence
(LCS) problem
• A string : A = b a c a d
• A subsequence of A: deleting 0 or more symbols
from A (not necessarily consecutive).
e.g. ad, ac, bac, acad, bacad, bcd.
• Common subsequences of A = b a c a d and
B = a c c b a d c b : ad, ac, bac, acad.
• The longest common subsequence (LCS) of A and
B:
a c a d.
The LCS algorithm
• Let A = a1 a2 … am and B = b1 b2 … bn
• Let Li,j denote the length of the longest common
subsequence of a1 a2 … ai and b1 b2 … bj.
• Li,j = Li-1,j-1 + 1 if ai=bj
max{ Li-1,j, Li,j-1 } if ai≠bj
L0,0 = L0,j = Li,0 = 0 for 1≤i≤m, 1≤j≤n.
• Time complexity: O(mn)
• The dynamic programming approach for solving
the LCS problem:
L1,1 L1,2 L1,3
L2,1 L2,2
L3,1
Lm,n
Tracing back in the LCS algorithm
• e.g. A = b a c a d, B = a c c b a d c b
B
a c c b a d c b
0 0 0 0 0 0 0 0 0
b 0 0 0 0 1 1 1 1 1
a 0 1 1 1 1 2 2 2 2
A c 0 1 2 2 2 2 2 3 3
a 0 1 2 2 2 3 3 3 3
d 0 1 2 2 2 3 4 4 4
Given a set of positive integers, A = {a1, a2, …, an}. The question is to select a subset
B of A such that the sum of the numbers in B equals the sum of the numbers not in
B, i.e., . We may assume that the sum of all numbers in A is 2K, an even
number. We now propose a dynamic programming solution. For 1 ≤ i ≤ n and 0 ≤ j
≤ K,
∑ ai = ∑ aj
ai ∈B a j ∈ A− B
Because j ≠ a1
j
i 0 1 2 3 4 5 6
a1 = 2 T F T F F F FT T T T
a2 = 1 F F FT T T T T F FT T
a3 = 1 T T T T TT T T T T T
a4 = 3 T P[4,5] = (P[3,5] or P[3,
a5 = 5 5 – a4] )
for(i=0;i<n;i++)
for(j=sum/2-A[i];j>=0;j--)
if(Flag[j]==1)
Flag[j+A[i]]=1;
String Partition
A string of digits instead of a list of integers. There are many ways to split a
string of digits into a list of non-zero-leading (0 itself is allowed) 32-bit
signed integers. What is the maximum sum of the resultant integers if the
string is split appropriately? A string of at most 200 digits.
Input:
11111111111111111111111111111111111111111111111111111
Output:
5555555666
Source Code
Limit = 2147483647; if(j==i&&input[j]==‘0’)
Len = strlen(input); break;
for(i=0;i<=Len;i++) }
Max[i]=0; }
for(i=0;i<Len;i++)
printf(“%lld\n”,Max[Len]);
{
Num = 0; Sample Problems:
for(j=i;j<Len;j++) •UVA Online Judge: 11258.
{
Num = Num * 10 + input[j]-’0’;
if(Num>Limit)
break;
if(Num+Max[i]>Max[j+1])
Max[j+1] = Num+Max[i];
The traveling salesperson (TSP)
problem
• e.g. a directed graph :
2
1 2
2
10
4
6 5 9 3
8
7
4 3
4
1 2 3 4
• Cost matrix: 1 ∞ 2 10 5
2 2 ∞ 9 ∞
3 4 3 ∞ 4
4 6 8 7 ∞
TSP
(1,2,3) 4 (1,2,3,4)
9
6
¡Û (1,2,4) 7 (1,2,4,3)
(1,2)
4
2
3 (1,3,2) ¡Û (1,3,2,4) 6
10
(1) (1,3) 1
2
4 (1,3,4) 8 (1,3,4,2)
5 4
(1,4) 9
8 (1,4,2) (1,4,2,3)
2
7
(1,4,3) 3 (1,4,3,2)