zoj 2027 Travelling Fee

2024-06-02 19:58
文章标签 zoj fee travelling 2027

本文主要是介绍zoj 2027 Travelling Fee,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题意:求从起点除法到终点的最少费用,题目比一般的最短路径多了个限制条件,可以免去花费最多的一条边的费用。


思路:用一个数组maxi[i][j]记录从i到j的路径中,一条路最多花费多少,将递推式改成 edge[i][j] = min (edge[i][k] + edge[k][j] - max(maxi[i][k],maxi[k][j]) , edge[i][j] - maxi[i][j]),当起点为s终点为t时,输出edge[s][t] - maxi[s][t]即可。


代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>using namespace std;struct node{int v;int w;node(int _v, int _w){v = _v;w = _w;}
};const int inf = 10000000;
const int maxn = 205;int m;
int n;//城市数 
int edge[maxn][maxn];	//
int maxi[maxn][maxn];	// 从i到j路径中的最大费用 
map<string,int>  Map;
string s,t;bool input(){if(!(cin>>s))//如果读取失败 return false;cin>>t;cin>>m;n = 0;Map.clear();for(int i = 0; i < maxn; i++) {for(int j = 0; j < maxn; j++){edge[i][j] = inf;maxi[i][j] = 0;}}string u,v;int w;for(int i = 0; i < m; i++){cin>>u>>v;cin>>w;if(Map[u] == 0) Map[u] = ++n;if(Map[v] == 0) Map[v] = ++n;int tu = Map[u];int tv = Map[v];edge[tu][tv] = w; maxi[tu][tv] = w;edge[tv][tu] = w; maxi[tv][tu] = w;}return true;
}void floyd(){for(int k = 1; k <= n; k++){for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){int t = max(maxi[i][k],maxi[k][j]);//最长的一段路 if(edge[i][k] + edge[k][j] - t < edge[i][j] - maxi[i][j]){edge[i][j] = edge[i][k] + edge[k][j];maxi[i][j] = t;	//更换免去费用的路段 }}}}
}void solve(){floyd();cout<<edge[Map[s]][Map[t]] - maxi[Map[s]][Map[t]]<<"\n";
}int main(){while(input()){solve();}return 0;
}


这篇关于zoj 2027 Travelling Fee的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1024943

相关文章

大数问题 ZOJ Problem Set - 2001 Adding Reversed Numbers

题目:   Time Limit: 2 Seconds       Memory Limit: 65536 KB The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancient plays are tragedies. Therefore t

ZOJ 3804 YY's Minions

ZOJ 3804 YY's Minions 简单的模拟题  初始时  每个点都有两种状态 0 1  然后判断每个点周围八个方向中相邻点的状态  判断状态为1的个数 注意点状态变为X是在每秒钟的末尾 所以要在每秒钟的末尾对状态进行改变 用两个数组对前一秒的状态跟这一秒的状态分别进行存储 #include <cstdio>#include <iostream>#include <cs

ZOJ 3798 Abs Problem

ZOJ 3798 Abs Problem  关于绝对值的一个题  题意是从1-N  N个不同的数中  选数 每次选一个数  第一次选的数记为a1  写到纸上记为b1   然后每次选的数ai   bi=|ai-bi-1|  求bn的最小值以及最大值 可以找规律 (1,2特判)  当模4的值等于0或3时  最小值为0  否则为1 n-1模4的值为0或3时   最大值为n  否则为n-1 注意

poj 1459 zoj 1734 Power Network(最大流)

电网 中一些节点  可能会消耗电能  提供电能   节点之间有电线传输电能  传输的电能有上限值 节点中没有 源点和汇点 类型的节点  所以需要我们添加源点汇点  n个节点 从0开始 设源点为n+1  汇点为n+2 源点到每个电站之间添加一条边 权值为该电站能够提供的电能 每个消费者与汇点之间添加一条边 权值为该消费者消费的电能 根据所给的 电线中的起点和重点 添加边 权值为该电线能够

zoj 1919 poj 2337 Catenyms(欧拉路径求解)

zoj 1919 poj 2337  Catenyms 前一个单词的尾字母与后一个单词的第一个字母相同  输出字典序最小的序列 用向量数组e[i]来存储以i开头的字符串  由于要是按字典序输出 所以需要讲字符串排序 f[i][j]数组表示 以第i个字母开头的第j个单词有没有被选取 level记录递归的层数  到达n之后 所有单词被选取 结束 yes标记有木有找到 找到了之后 递归

poj 2396 zoj 1994 Budget(有源汇上下界的可行流)

思路: 无源汇 (附加源汇+最大解决) 有源汇 (附加(T,S)->无源汇) Budget Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 6237 Accepted: 2367 Special Judge Description We are supposed to make

TOJ 4365 ZOJ 3623 Battle Ships / 完全背包

Battle Ships 时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte 描述 Battle Ships is a new game which is similar toStar Craft. In this game, the enemy builds a defense tower, which has L longe

TOJ 4369 ZOJ 3632 Watermelon Full of Water / 线段树优化DP

Watermelon Full of Water 时间限制(普通/Java):3000MS/9000MS     运行内存限制:65536KByte 描述 Watermelon is very popular in the hot summer. Students in ZJU-ICPC Team also love watermelon very much and they

ZOJ 3696 Alien's Organ / 泊松分布

有遇到 奇葩的 前所未闻的东西 记一下 观察事物平均发生m次的条件下,实际发生x次的概率P(x) p(k)=(y^k) / (k!) * e^(-y) 吊炸天 这种东西做梦都想不到 平均每天生产的λ 个,每天生产不超过n的可能性 代码参考别人的 #include <cstring>#include <cstdio>#include <cmath>int main(){int T

ZOJ 3557 How Many Sets II lucas 定理

插空法 大组合数取余 #include <cstdio>#include <cstring>using namespace std;typedef long long LL;//求整数x和y,使得ax+by=d, 且|x|+|y|最小。其中d=gcd(a,b) void gcd(LL a, LL b, LL& d, LL& x, LL& y){if(!b){d = a;x = 1;