ural 1741 Communication Fiend

//ural 1741 Communication Fiend
//http://acm.timus.ru/problem.aspx?space=1&num=1741
/*
 * 题意:
 * 原有Licensed的版本1一份,欲升级到版本n,现有m份升级程序,每一份指定原始版本x,升级后版本y,代价cost和程序类型type
 * Licensed只能作用在Licensed上,Pirated 能作用在任何类型上,但会把升级后的变为Pirated,Cracked 能作用在任何类型上,不改变程序类型
 * 输出Online和最小升级代价或者Offline表示不能升级
 * 思路:
 * DP,先把M个升级程序按照x升序排序,然后逐个应用,d[i][0]表示升级到i的Licensed需要的最小代价,d[i][1]则表示升级到i的Pirated的最小代价
 * author:licatweijie
 */
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 10010
#define LOL long long
using namespace std;

LOL d[N][2];
struct up{
    int x,y;
    int cost;
    int type;
    bool operator<(const up& rhs)const {
        return x<rhs.x;
    }
}ups[N];

int n,m;

int get_second(int x,int now){
    if (ups[x].type==2)
        return 1;
    if (ups[x].type==1)
        return now;
    else return 0;
}

int main()
{
    std::ios::sync_with_stdio(false);
    char ty[10];
    while(cin >> n >> m){
        for (int i=0;i<m;i++){
            cin >> ups[i].x >> ups[i].y >> ups[i].cost >> ty;
            if (ty[0]=='L'){
                ups[i].type = 0;
            }else if (ty[0]=='C'){
                ups[i].type = 1;
            }else ups[i].type = 2;
        }
        sort(ups,ups+m);
        memset(d,0,sizeof(d));
        d[1][0] = 1;
        for (int i =0;i<m;i++){
            if (d[ups[i].x][0]!=0){
                //正版前置存在
                if (d[ups[i].y][get_second(i,0)]==0 || d[ups[i].x][0]+ups[i].cost < d[ups[i].y][get_second(i,0)]){
                    d[ups[i].y][get_second(i,0)] = d[ups[i].x][0]+ups[i].cost;
                }
            }

            if (d[ups[i].x][1]!=0){
                if (ups[i].type==0) continue;
                //前置存在
                if (d[ups[i].y][1]==0 || d[ups[i].x][1]+ups[i].cost < d[ups[i].y][1] )
                    d[ups[i].y][1] = d[ups[i].x][1]+ups[i].cost;
            }
        }
        LOL ans = 0;
        if (d[n][0]!=0)
            ans = d[n][0];
        if (ans==0|| (d[n][1]!=0 && d[n][1]<ans  )){
            ans = d[n][1];
        }
        if (ans!=0){
            cout << "Online" << endl;
            cout << ans-1 << endl;
        } else {
            cout << "Offline" << endl;
        }
    }

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值