zoukankan      html  css  js  c++  java
  • CH6101 最优贸易【最短路】

    6101 最优贸易 0x60「图论」例题

    描述

    C国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市。任意两个城市之间最多只有一条道路直接相连。这 m 条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向通行的道路在统计条数时也计为1条。
    C国幅员辽阔,各地的资源分布情况各不相同,这就导致了同一种商品在不同城市的价格不一定相同。但是,同一种商品在同一个城市的买入价和卖出价始终是相同的。
    商人阿龙来到C国旅游。当他得知“同一种商品在不同城市的价格可能会不同”这一信息之后,便决定在旅游的同时,利用商品在不同城市中的差价赚一点旅费。设C国 n 个城市的标号从 1~n,阿龙决定从1号城市出发,并最终在 n 号城市结束自己的旅行。在旅游的过程中,任何城市可以被重复经过多次,但不要求经过所有 n 个城市。
    阿龙通过这样的贸易方式赚取旅费:他会选择一个经过的城市买入他最喜欢的商品——水晶球,并在之后经过的另一个城市卖出这个水晶球,用赚取的差价当做旅费。因为阿龙主要是来C国旅游,他决定这个贸易只进行最多一次,当然,在赚不到差价的情况下他就无需进行贸易。
    现在给出 n 个城市的水晶球价格,m 条道路的信息(每条道路所连接的两个城市的编号以及该条道路的通行情况)。请你告诉阿龙,他最多能赚取多少旅费。

    输入格式

       第一行包含 2 个正整数n 和m,中间用一个空格隔开,分别表示城市的数目和道路的
    数目。
       第二行 n 个正整数,每两个整数之间用一个空格隔开,按标号顺序分别表示这n 个城
    市的商品价格。
       接下来 m 行,每行有3 个正整数,x,y,z,每两个整数之间用一个空格隔开。如果z=1,表示这条道路是城市x 到城市y 之间的单向道路;如果z=2,表示这条道路为城市x 和城市y 之间的双向道路。

    输出格式

    一个整数,表示答案。

    样例输入

    5 5
    4 3 5 6 1
    1 2 1
    1 4 1
    2 3 2
    3 5 1
    4 5 2
    

    样例输出

    5

    数据范围与约定

    • 输入数据保证 1 号城市可以到达n 号城市。
      对于 10%的数据,1≤n≤6。
      对于 30%的数据,1≤n≤100。
      对于 50%的数据,不存在一条旅游路线,可以从一个城市出发,再回到这个城市。
      对于 100%的数据,1≤n≤100000,1≤m≤500000,1≤x,y≤n,1≤z≤2,1≤各城市
      水晶球价格≤100。

    来源

    CCF NOIP2009

    题意:

    n个城市水晶球的价格各不相同,从$1~N$的路径中选择价格最低的城市买一个水晶球,价格最高的城市卖一个水晶球。问得到的收益最大是多少。

    思路:

    说是最短路其实就是一个bfs吧。

    假设经过了城市$i$,那么这一段路程的最大收益就是$i~N$路径中的最大价格减$1~i$路径中的最小价格。

    所以我们还需要建一个反向的图。

    第一次以城市$1$为起点找到从$1$出发到各个点时最小的价格。

    然后以城市$N$为起点跑反向找到各个点的最大价格,其实也就是从各个点跑到$N$的过程中的最大价格。

    然后遍历每个点,找到差值最大的即为答案。

      1 #include<iostream>
      2 //#include<bits/stdc++.h>
      3 #include<cstdio>
      4 #include<cmath>
      5 #include<cstdlib>
      6 #include<cstring>
      7 #include<algorithm>
      8 #include<queue>
      9 #include<vector>
     10 #include<set>
     11 #include<climits>
     12 using namespace std;
     13 typedef long long LL;
     14 #define N 100010
     15 #define pi 3.1415926535
     16 
     17 int n, m;
     18 const int maxn = 1e5 + 5;
     19 const int maxm = 5e5 + 5;
     20 int price[maxn];
     21 vector<int>graph[maxn];
     22 vector<int>fangraph[maxn];
     23 int buy[maxn], sell[maxn];
     24 bool vis[maxn];
     25 
     26 void dijkstra()
     27 {
     28     /*for(int i = 1; i <= n; i++){
     29         buy[i] = price[i];
     30     }*/
     31     //memset(buy, 0x3f, sizeof(buy));
     32     memset(vis, 0, sizeof(vis));
     33     buy[1] = price[1];vis[1] = true;
     34     queue<int >que;
     35     que.push(1);
     36     while(que.size()){
     37         int x = que.front();que.pop();
     38         for(int i = 0; i < graph[x].size(); i++){
     39             int y = graph[x][i];
     40             if(!buy[y])buy[y] = price[y];
     41             buy[y] = min(buy[y], buy[x]);
     42             if(!vis[y]){
     43                 que.push(y);
     44                 vis[y] = true;
     45             }
     46             /*if(buy[y] > buy[x]){
     47                 buy[y] = buy[x];
     48                 que.push(make_pair(-buy[y], y));
     49             }*/
     50         }
     51     }
     52     //return buy[n];
     53 }
     54 
     55 void fandijkstra()
     56 {
     57     //memset(sell, 0x3f, sizeof(sell));
     58     /*for(int i = 1; i <= n; i++){
     59         sell[i] = price[i];
     60     }*/
     61     memset(vis, 0, sizeof(vis));
     62     sell[n] = price[n];vis[n] = true;
     63     queue<int >que;
     64     que.push(n);
     65     while(que.size()){
     66         int x = que.front();que.pop();
     67         for(int i = 0; i < fangraph[x].size(); i++){
     68             int y = fangraph[x][i];
     69             if(!sell[y])sell[y] = price[y];
     70             sell[y] = max(sell[y], sell[x]);
     71             if(!vis[y]){
     72                 que.push(y);
     73                 vis[y] = true;
     74             }
     75             /*if(sell[y] < sell[x]){
     76                 sell[y] = sell[x];
     77                 que.push(make_pair(sell[y], y));
     78             }*/
     79         }
     80     }
     81     //return sell[1];
     82 }
     83 
     84 int main()
     85 {
     86     //freopen("in.txt", "r", stdin);
     87     scanf("%d%d", &n, &m);
     88     for(int i = 1; i <= n; i++){
     89         scanf("%d", &price[i]);
     90     }
     91     for(int i = 0; i < m; i++){
     92         int u, v, t;
     93         scanf("%d%d%d", &u, &v, &t);
     94         graph[u].push_back(v);
     95         fangraph[v].push_back(u);
     96         if(t == 2){
     97             graph[v].push_back(u);
     98             fangraph[u].push_back(v);
     99         }
    100     }
    101 
    102     int ans = 0;
    103     dijkstra();
    104     /*for(int i = 1; i <= n; i++){
    105         printf("%d ", buy[i]);
    106     }
    107     cout<<endl;*/
    108     fandijkstra();
    109     /*for(int i = 1; i <= n; i++){
    110         printf("%d ", sell[i]);
    111     }
    112     cout<<endl;*/
    113     for(int i = 1; i <= n; i++){
    114         ans = max(ans, sell[i] - buy[i]);
    115     }
    116     printf("%d
    ", ans);
    117     return 0;
    118 }
  • 相关阅读:
    go入门4---数据
    hibernate之关联关系一对多
    hibernate的主键生成策略
    hibernate的入门
    struts--CRUD优化(图片上传)
    struts2--CRUD
    struts2--入
    Maven环境搭建
    EasyUI--增删改查
    easyui--权限管理
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9968924.html
Copyright © 2011-2022 走看看