码迷,mamicode.com
首页 > 其他好文 > 详细

最优贸易(最短路应用)

时间:2019-04-16 00:57:51      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:卖出   memset   cst   clu   eof   class   style   双向   deb   

题目描述

C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市。任意两个城市之间最多只有一条道路直接相连。这 m 条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向通行的道路在统计条数时也计为 1 条。

C 国幅员辽阔,各地的资源分布情况各不相同,这就导致了同一种商品在不同城市的价格不一定相同。但是,同一种商品在同一个城市的买入价和卖出价始终是相同的。

商人阿龙来到 C 国旅游。当他得知同一种商品在不同城市的价格可能会不同这一信息之后,便决定在旅游的同时,利用商品在不同城市中的差价赚回一点旅费。设 C 国 n 个城市的标号从 1~ n,阿龙决定从 1 号城市出发,并最终在 n 号城市结束自己的旅行。在旅游的过程中,任何城市可以重复经过多次,但不要求经过所有 n 个城市。阿龙通过这样的贸易方式赚取旅费:他会选择一个经过的城市买入他最喜欢的商品――水晶球,并在之后经过的另一个城市卖出这个水晶球,用赚取的差价当做旅费。由于阿龙主要是来 C 国旅游,他决定这个贸易只进行最多一次,当然,在赚不到差价的情况下他就无需进行贸易。

假设 C 国有 5 个大城市,城市的编号和道路连接情况如下图,单向箭头表示这条道路为单向通行,双向箭头表示这条道路为双向通行。

技术图片

假设 1~n 号城市的水晶球价格分别为 4,3,5,6,1。

阿龙可以选择如下一条线路:1->2->3->5,并在 2 号城市以 3 的价格买入水晶球,在 3号城市以 5 的价格卖出水晶球,赚取的旅费数为 2。

阿龙也可以选择如下一条线路 1->4->5->4->5,并在第 1 次到达 5 号城市时以 1 的价格买入水晶球,在第 2 次到达 4 号城市时以 6 的价格卖出水晶球,赚取的旅费数为 5。

现在给出 n 个城市的水晶球价格,m 条道路的信息(每条道路所连接的两个城市的编号以及该条道路的通行情况)。请你告诉阿龙,他最多能赚取多少旅费。

输入输出格式

输入格式:

第一行包含 2 个正整数 n 和 m,中间用一个空格隔开,分别表示城市的数目和道路的数目。

第二行 n 个正整数,每两个整数之间用一个空格隔开,按标号顺序分别表示这 n 个城市的商品价格。

接下来 m 行,每行有 3 个正整数,x,y,z,每两个整数之间用一个空格隔开。如果 z=1,表示这条道路是城市 x 到城市 y 之间的单向道路;如果 z=2,表示这条道路为城市 x 和城市y 之间的双向道路。

输出格式:

输出文件 trade.out 共 1 行,包含 1 个整数,表示最多能赚取的旅费。如果没有进行贸易,则输出 0。

输入输出样例

输入样例#1: 复制
5 5 
4 3 5 6 1 
1 2 1 
1 4 1 
2 3 2 
3 5 1 
4 5 2 
输出样例#1: 复制
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。

树dp写法:

//2018年4月30日12:20:22
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int N = 100001;
const int M = 500001; 

const int INF = 1e9+7; 

int n, m;
int w[N];

int fir[N], nxt[M], to[M], edge_num;
void addEdge(int x, int y){
    to[++edge_num] = y;
    nxt[edge_num] = fir[x];
    fir[x] = edge_num;
}

int f[N], mi[N];

void dfs(int x, int minx, int pre){
    int flag = 1;
    minx = min(w[x], minx);
    if(mi[x] > minx) mi[x] = minx, flag = 0;
    int maxx = max(f[pre], w[x]-minx);
    if(f[x] < maxx) f[x] = maxx, flag = 0;
    if(flag) return;
    for(int i=fir[x]; i; i=nxt[i]) dfs(to[i], minx, x);
}

int main(){
    scanf("%d%d", &n, &m); 
    for(int i=1; i<=n; i++)
        scanf("%d", &w[i]);
    for(int i=1; i<=N; i++)
        mi[i] = INF;
    for(int i=1; i<=m; i++){
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        if(z == 1){
            addEdge(x, y); 
        }else if(z == 2){
            addEdge(x, y); addEdge(y, x);
        }
    }
    
    dfs(1, INF, 0);
    
    printf("%d\n", f[n]);

    return 0;
}

最短路写法:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn=1e5+5;
const int maxm=5e5+5;
int first[maxn][2],next[maxm][2],to[maxm][2],w[maxn];
int edge_count[2];
inline void add(int x,int y,int b){
//    printf("%d %d %d\n",x,y,b);
    edge_count[b]++;
    to[ edge_count[b] ][b]=y;
    next[ edge_count[b] ][b]=first[x][b];
    first[x][b]=edge_count[b]; 
}
int n,m,ans=0;
inline void read(int &a){
    a=0;int b=1;char x=getchar();
    while(x<0||9<x){
        if(x==-)b=-1;
        x=getchar();
    }
    while(0<=x&&x<=9){
    a=(a<<3)+(a<<1)+x-0;
    x=getchar();
    }
    a*=b;
}
bool vis[maxn];
int dis[maxn][2],queue[maxm];
inline void spfa(int s,int b){
    memset(vis,0,sizeof(vis));
    
    if(b==0)for(int i=1;i<=n;i++)dis[i][0]=1e8;//min
    else    for(int i=1;i<=n;i++)dis[i][1]=-1e8; //max
    
    dis[s][b]=w[s];
    vis[s]=1;
    
    int front=1,rear=0;
    queue[rear]=s;
    while(rear<front){
        int pos=queue[rear];
        //if(b)printf("%d:\n",pos);
        for(int i=first[pos][b];i;i=next[i][b]){
            
            int v=to[i][b];
        //    printf("%d ",v);
            if(b==0){
            int t=min(dis[pos][b],w[ v ]);
            if(t<dis[ v ][0]){
                dis[ v ][0]=t;
                if(!vis[ v ]){
                    vis[ v ]=1;
                    queue[front]=v;front++;
                }
            }
        }
            else{
            int t=max(dis[pos][1],w[ v ]);
            //printf("%d %d ",pos,t);
            if(t>dis[ v ][1]){
                dis[ v ][1]=t;
                if(!vis[ v ]){
                    vis[ v ]=1;
                    queue[front]=v;front++;
                }
            }
        }
    }
        vis[ pos ]=0;
        rear++;
        }//老子发誓以后缩进一定好好打,要不然debug非常傻逼
        
}
int main()
{
     read(n);read(m);
     for(int i=1;i<=n;i++){
         read(w[i]);//printf("%d ",w[i]);
    }
    for(int i=1,x,y,c;i<=m;i++){
        read(x);read(y);read(c);
        add(x,y,0);//正向 
        add(y,x,1);//反向 
        if(c==2){
        add(y,x,0);
        add(x,y,1);
        }
    }
    spfa(1,0);
    spfa(n,1);
    for(int i=1;i<=n;i++){
    ans=max(ans,dis[i][1]-dis[i][0]);
    //printf("%d %d %d\n",i,dis[i][0],dis[i][1]);
    }
    printf("%d",ans);
    return 0;
}

 

最优贸易(最短路应用)

标签:卖出   memset   cst   clu   eof   class   style   双向   deb   

原文地址:https://www.cnblogs.com/a-blog-of-taojiayi-2003/p/10714280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!