URAL1741——DP——Communication Fiend

Description

Kolya has returned from a summer camp and now he's a real  communication fiend. He spends all his free time on the Web chatting with his friends via ICQ. However, lately the protocol of this service was changed once again, and Kolya's client stopped working. Now, in order to communicate with his friends again, Kolya has to upgrade his client from version 1 to version  n.
Kolya has found  m upgrade programs on the Web. The  i-th program upgrades the client from version  x i to version  y i and its size is  d imegabytes. Each program can be installed on the corresponding version of the client only; it can't be installed on either earlier or later versions.
The first version, which is currently installed on Kolya's computer, is licensed, and many of the upgrade programs are pirate copies. If a pirate upgrade program is used, the client will always be pirated after that, whatever upgrade is used later. Some of the licensed upgrade programs can be installed on a pirate version of the client, and some of them can't. All the pirate upgrade programs can be installed on both licensed and pirate versions of the client.
Kolya is missing his friends very much, so he wants to download the necessary upgrade programs as soon as possible. Unfortunately, his Web connection is not very fast. Help Kolya determine the minimal total traffic volume necessary for upgrading the client from version 1 to version  n. Kolya doesn't care if the final version  n of his client is licensed or not.

Input

The first line contains space-separated integers  n and  m (2 ≤   n ≤ 10  4; 1 ≤   m ≤ 10  4).
Each of the following  m lines describes one upgrade program in the form “  x i y i d i s i”. Here,  s i is the type of the program: “Pirated”, “Cracked”, or “Licensed”. A cracked upgrade program is a licensed program that can be installed on a pirate version of the client, and a licensed program can't be installed on a pirate version. The numbers  x i and  y i mean that the program is installed on version  x i of the client and upgrades it to version  y i. The number  d i is the size of the program in megabytes (1 ≤   x i <   y i ≤   n; 1 ≤   d i ≤ 10  6). The data in each line are separated with exactly one space.

Output

If Kolya can upgrade the client from version 1 to version  n, output “Online” in the first line and the minimal necessary total incoming traffic volume in the second line.
If it is impossible to upgrade the client, output “Offline”.

Sample Input

input output
3 4

1 3 10 Licensed

1 2 2 Pirated

2 3 3 Licensed

2 3 6 Cracked

Online

8

3 1

1 2 10 Licensed

Offline

大意:系统要从1更新到n,下面m行是可以进行更新的情况,x表示起点,y表示终点,d表示更新所要用的字节数目,问所需要消耗的最少字节数是多少。

先要对x进行排序,字符串处理未必需要strcmp,直接用ch[0]就可以了。

正品用正品更新完是正品,用盗版更新完是盗版,用破解版更新还是正品

盗版用盗版更新是盗版,用正品更新还是盗版,用破解版依旧是盗版

开个二维dp   1表示正品,2表示盗版

状态转移方程  dp[a[i].y] = min(dp[a[i].y],dp[a[i].x]+a[i].s)

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

struct edge{

    int x,y,s;

    char ch[10];

}a[50010];

long long  dp[50010][2];

bool cmp(edge i,edge j){

    if(i.x == j.x) return i.y < j.y;

    return i.x < j.x;

}

int main()

{

    int n,m;

    while(~scanf("%d%d",&n,&m)){

        for(int i = 1; i <= m ; i++)

            scanf("%d%d%d%s",&a[i].x,&a[i].y,&a[i].s,&a[i].ch);

        sort(a+1,a+m+1,cmp);

        memset(dp,-1,sizeof(dp));

        dp[1][1] = 0;

        for(int i = 1;i <= m ;i++){

            if(a[i].ch[0] == 'L'){

                if(dp[a[i].x][1] != -1 &&(dp[a[i].y][1] == -1 || dp[a[i].y][1] > dp[a[i].x][1] + a[i].s))

                dp[a[i].y][1] = dp[a[i].x][1] + a[i].s;

            }

           else if(a[i].ch[0] == 'C'){

               if(dp[a[i].x][1] != -1 &&(dp[a[i].y][1] == -1 || dp[a[i].y][1] > dp[a[i].x][1] + a[i].s))

                dp[a[i].y][1] = dp[a[i].x][1] +a[i].s;

               if(dp[a[i].x][0] != -1 &&(dp[a[i].y][0] == -1 || dp[a[i].y][0] > dp[a[i].x][0] + a[i].s))

                dp[a[i].y][0] = dp[a[i].x][0] + a[i].s;

           }

               else if(a[i].ch[0] == 'P'){

                   if(dp[a[i].x][0] != -1 && (dp[a[i].y][0] == -1 || dp[a[i].y][0] > dp[a[i].x][0] + a[i].s))

                       dp[a[i].y][0] = dp[a[i].x][0] + a[i].s;

                   if(dp[a[i].x][1] != -1 && (dp[a[i].y][0] == -1 || dp[a[i].y][0] > dp[a[i].x][1] + a[i].s))

                        dp[a[i].y][0]  = dp[a[i].x][1] + a[i].s;

               }

        }

        if(dp[n][0] == -1 && dp[n][1] == -1)

            printf("Offline\n");

        else {

            printf("Online\n");

            if(dp[n][0] == -1)

                printf("%lld\n",dp[n][1]);

            else if(dp[n][1] == -1)

                printf("%lld\n",dp[n][0]);

            else printf("%lld\n",min(dp[n][1],dp[n][0]));

        }

    }

    return 0;

}

  

你可能感兴趣的:(com)