zoukankan      html  css  js  c++  java
  • 图论——最短路①

    RT

    找工就业

    Bessie is running out of money and is searching for jobs. Farmer John knows this and wants the cows to travel around so he has imposed a rule that his cows can only make D (1 <= D <= 1,000) dollars in a city before they must work in another city. Bessie can, however, return to a city after working elsewhere for a while and again earn the D dollars maximum in that city. There is no limit on the number of times Bessie can do this.

    Bessie's world comprises P (1 <= P <= 150) one-way paths connecting C (2 <= C <= 220) cities conveniently numbered 1..C. Bessie is currently in city S (1 <= S <= C). Path i runs one-way from city A_i to city B_i (1 <= A_i <= C; 1 <= B_i <= C) and costs nothing to traverse.

    To help Bessie, Farmer John will give her access to his private jet service. This service features F (1 <= F <= 350) routes, each of which is a one way flight from one city J_i to a another K_i (1 <= J_i <= C; 1 <= K_i <= C) and which costs T_i (1 <= T_i <= 50,000) dollars. Bessie can pay for the tickets from future earnings if she doesn't have the cash on hand.

    Bessie can opt to retire whenever and wherever she wants. Given an unlimited amount of time, what is the most money that Bessie can make presuming she can make the full D dollars in each city she can travel to? Print -1 if there is no limit to this amount.

    奶牛们正在找工作。农场主约翰知道后,鼓励奶牛们四处碰碰运气。而且他还加了一条要求:一头牛在一个城市最多只能赚D(1≤D≤1000)美元,然后它必须到另一座城市工作。当然,它可以在别处工作一阵子后又回到原来的城市再最多赚D美元。而且这样的往返次数没有限制。

    城市间有P(1≤P≤150)条单向路径连接,共有C(2≤C≤220)座城市,编号从1到C。奶牛贝茜当前处在城市S(1≤S≤C)。路径i从城市A_i到城市B_i(1≤A_i≤C,1≤B_i≤C),在路径上行走不用任何花费。

    为了帮助贝茜,约翰让它使用他的私人飞机服务。这项服务有F条(1≤F≤350)单向航线,每条航线是从城市J_i飞到另一座城市K_i(1≤J_i≤C,1≤K_i≤C),费用是T_i(1≤T_i≤50000)美元。如果贝茜手中没有现钱,可以用以后赚的钱来付机票钱。

    贝茜可以选择在任何时候,在任何城市退休。如果在工作时间上不做限制,贝茜总共可以赚多少钱呢?如果赚的钱也不会出现限制,就输出-1。

    输入输出格式

    输入格式:

    第一行:5个用空格分开的整数D,P,C,F,S。

    第2到第P+1行:第i+1行包含2个用空格分开的整数,表示一条从城市A_i到城市B_i的单向路径。

    接下来F行,每行3个用空格分开的整数,表示一条从城市J_i到城市K_i的单向航线,费用是T_i。

     输出格式:

     一个整数,在上述规则下最多可以赚到的钱数。

    样例

    输入:          输出:

    100 3 5 2 1            250
    1 5
    2 3
    1 4
    5 2 150
    2 5 120

    好,一读题就知道是一道 裸题 好 水 题;图论基础

    感觉就是个最长路加个判负环;也没什么;

    #include<cstdio>
    #include<algorithm>
    #include<queue>
    #include <cstring>
    using namespace std;
    struct data{int v;int nxt;int val;}edge[500010];
    int cnt;int alist[10010];
    inline void add(int u,int v,int val)
    {
        edge[++cnt].v=v;edge[cnt].nxt=alist[u];
        edge[cnt].val=val;alist[u]=cnt;
    }
    int d[10010];bool vis[100010];
    int dol,p,c,f,s,ans;
    int tim[100010];
    queue <int> q;
    int main()
    {
       // freopen("in","r",stdin);
        scanf("%d%d%d%d%d",&dol,&p,&c,&f,&s);
        for(int i=1;i<=p;++i)
        {
            int u;int v;
            scanf("%d%d",&u,&v);
            add(u,v,dol);
        }
        for(int i=1;i<=f;++i)
        {
            int a,b,val;
            scanf("%d%d%d",&a,&b,&val);
            add(a,b,dol-val);
        }
        for(int i=1;i<=c;i++){d[i]=-0x3f3f3f3f;}
        d[s]=dol;q.push(s);
        while(!q.empty())
        {
            int now=q.front();q.pop();vis[now]=false;
            int nxt=alist[now];
            while(nxt)
            {
                int v=edge[nxt].v;
                int val=edge[nxt].val;
                if(d[v]<d[now]+val)
                {
                    d[v]=d[now]+val;
                    tim[v]++;               //判负环
                    if(tim[v]>c)
                    {
                        printf("-1");
                        return 0;
                    }
                    if(!vis[v]){q.push(v);vis[v]=true;}
                }
                nxt=edge[nxt].nxt;
            }
        }
        for(int i=1;i<=c;i++)
        {
            ans=max(ans,d[i]);
        }
        printf("%d",ans);
    }    
    View Code

                                                                              2018-02-07

  • 相关阅读:
    lintcode42- Maximum Subarray II- medium
    leetcode53- Maximum Subarray- easy
    leetcode50- Pow(x, n)- medium
    leetcode23- Merge k Sorted Lists- hard
    leetcode21- Merge Two Sorted Lists- easy
    lintcode121- Word Ladder II- hard
    lintcode107- Word Break- medium
    lintcode10- Permutation Index II- medium
    AM335x关于LCD屏幕的时钟PLL配置 分类: TI-AM335X 2015-06-16 18:32 341人阅读 评论(0) 收藏
    用DriverStudio开发USB驱动程序 分类: USB OTG驱动 2015-06-12 10:34 376人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/AidenPearce/p/8425402.html
Copyright © 2011-2022 走看看