nefu1132

Cost of traveling

Problem:1132

Time Limit:1000ms

Memory Limit:65535K

Description

Xiao Ming is in a bad mood these days.So he wants to relieve himself by travelling. He does not want to choose train or coach , then he makes a significant decision: travel by air! Fortunately, he is provided k opportunities to have 50% discount for tickets. It means that he only needs to pay 50 yuan for the ticket which is worth 100 yuan and only needs to pay 49 yuan for the ticket which is worth 99 yuan. He is in the city s and wants to travel to city t. How much does it cost at least to get to city t if he makes full use of the opportunities for discount?

Input

The first line includes three integers: s, t, k(0<=s<100, 0<=t<100, 0<=k<100).The meanings of them have been mentioned above.
The second line is an integer N, N<=2000. It means that you will be informed of the information of N flights.
From the third line to the N+2 line, there are three integers each line: u,v and p. They represent the the flight from u to v costs p yuan. Please notice that the flight is unidirectional and there is not only one flight from u to v. 0<=u<100, 0<=v<100, 0<=p<10000.

Output

One line contain an integer which is the amount of money you need in order to fly to t.
If you can not reach the city t, please output “KengDie”.

Sample Input

0 3 1
5
0 1 10
1 2 10
2 3 10
0 4 1
4 3 30

0 3 1
1
0 1 10

0 1 10
1
0 1 10

Sample Output

16
KengDie
5
题意:由s出发点到t终点,要经过一些路,每条路都有一个收费站,现在给你k个的半价优惠机会,你可以在任何一格收费站使用,问最小花费。
dp更新最小花费,dp[i][j]表示到达i,剩余j次机会,从t出发,向回搜索,直到出发点,记忆搜索加减枝。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
const int N=105,inf=0x3f3f3f3f;
int s,t,k,n;
int map[N][N],dp[N][N];
int dfs(int v,int j)///(t,k),dp[i][j]表示到达i,剩余j次机会
{
    if(j<0)
        return inf;
    if(v==s)
        return dp[v][j]=0;
    if(dp[v][j])
        return dp[v][j];

    dp[v][j]=inf;///初始化为最大值
    for(int u=0; u<=101; u++)
    {
        if(u==v)
            continue;
        if(map[u][v]!=inf)///由t终点想s出发点搜,
     dp[v][j]=min( dp[v][j], min( dfs(u,j)+map[u][v],dfs(u,j-1)+(map[u][v])/2));

    }
    return dp[v][j];
}
int main()
{
    int u,v,c;
    while(~scanf("%d%d%d",&s,&t,&k))
    {
        scanf("%d",&n);
        for(int i=0; i<=N; i++)
            for(int j=0; j<=N; j++)
                map[i][j]=inf;
        while(n--)
        {
            scanf("%d%d%d",&u,&v,&c);
            map[u][v]=min(map[u][v],c);
        }
        memset(dp,0,sizeof(dp));
        int ans=dfs2(s,k);
        if(ans==inf)
            cout<<"KengDie"<<endl;
        else
            cout<<ans<<endl;

    }
    return 0;
}
<br /><span id="_xhe_temp" width="0" height="0"><br /></span>

你可能感兴趣的:(nefu1132)