【HDU - 3499】 Flight (单源最短路+优惠问题)

题干:

Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to some other city to avoid meeting her. He will travel only by air and he can go to any city if there exists a flight and it can help him reduce the total cost to the destination. There's a problem here: Shua Shua has a special credit card which can reduce half the price of a ticket ( i.e. 100 becomes 50, 99 becomes 49. The original and reduced price are both integers. ). But he can only use it once. He has no idea which flight he should choose to use the card to make the total cost least. Can you help him?

Input

There are no more than 10 test cases. Subsequent test cases are separated by a blank line. 
The first line of each test case contains two integers N and M ( 2 <= N <= 100,000 

0 <= M <= 500,000 ), representing the number of cities and flights. Each of the following M lines contains "X Y D" representing a flight from city X to city Y with ticket price D ( 1 <= D <= 100,000 ). Notice that not all of the cities will appear in the list! The last line contains "S E" representing the start and end city. X, Y, S, E are all strings consisting of at most 10 alphanumeric characters. 

Output

One line for each test case the least money Shua Shua have to pay. If it's impossible for him to finish the trip, just output -1.

Sample Input

4 4
Harbin Beijing 500
Harbin Shanghai 1000
Beijing Chengdu 600
Shanghai Chengdu 400
Harbin Chengdu4 0
Harbin Chengdu

Sample Output

800
-1

Hint

In the first sample, Shua Shua should use the card on the flight fromBeijing to Chengdu, making the route Harbin->Beijing->Chengdu have theleast total cost 800. In the second sample, there's no way for him to get to 
Chengdu from Harbin, so -1 is needed. 

题目大意:

     有一个有向图,你要从特定的城市A飞到城市B去.给你这个图的所有边(航班)信息.但是你手上有一张卡,可以使得某一趟航班的价格减半.现在的问题是你从A到B的最小费用是多少?

解题报告:

     首先要知道这条如果让一条原本是最短路径(假设总距离为x)上最长的边变成半价,最终求得的解不一定是最优的。因为假如现在有另外一条路径,假设该路径距离为x+1。且这条路径上只有5条边,4条长为1的边,但是1条长为x-3的边。如果我们让这条路径的x-3边变成半价是不是能得到更好的结果?

        明显必须从m条边中枚举那条半价的航班.假设这条半价的航班是i->j的.那么我们必须知道从A到i的最短距离和从j到B的最短距离. 从A到i的最短距离可以通过求A的单源最短路径即可.从j(j有可能是任意点)到B的最短距离必须建立原图的反向图,然后求B到其他所有点的单源最短路径.(想想是不是)

        原题输入数据很多,需要用邻接表的dijkstra算法且距离要用long long保存.

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
//const int INF = 0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
int n,m;
int cnt1,cnt2,top;
map<string,int > mp;
ll dis1[100000 + 5],dis2[100000 + 5];
int head1[100000 + 5],head2[100000 + 5],cntt[100000 + 5];
bool vis[100000 + 5];
int st,ed;struct Edge {int pos;int to;ll w;int ne;
}e[500000 + 5],E[500000 + 5];
struct point {int pos;ll c;point(){}//没有此构造函数不能写  node t  这样point(int pos,int c):pos(pos),c(c){}//可以写node(pos,cost)这样bool operator <(const point & b) const {return c>b.c;}
};void add1(int u,int v,ll w) {e[cnt1].pos = u;e[cnt1].to = v;e[cnt1].w = w;e[cnt1].ne = head1[u];head1[u] = cnt1++;
}
void add2(int u,int v,ll w) {E[cnt2].pos = u;E[cnt2].to = v;E[cnt2].w = w;E[cnt2].ne = head2[u];head2[u] = cnt2++;
}
int Dijkstra1(int u,int v) {priority_queue<point> pq; for(int i = 1; i<=n; i++) dis1[i] = INF;memset(vis,0,sizeof(vis));dis1[u] = 0;point cur = point(u,0);pq.push(cur);while(!pq.empty()) {point now = pq.top();pq.pop();vis[now.pos] = 1;
//		if(now.pos == v) break;for(int i = head1[now.pos]; i!=-1; i=e[i].ne) {if(  dis1[e[i].to] > dis1[now.pos] + e[i].w ) {dis1[e[i].to] = dis1[now.pos] + e[i].w;pq.push(point(e[i].to,dis1[e[i].to] ) );	}} }if(dis1[v] == INF) return -1;else return dis1[v];
}
int Dijkstra2(int u,int v) {priority_queue<point> pq; for(int i = 1; i<=n; i++) dis2[i] = INF;memset(vis,0,sizeof(vis));dis2[u] = 0;point cur = point(u,0);pq.push(cur);while(!pq.empty()) {point now = pq.top();pq.pop();vis[now.pos] = 1;
//		if(now.pos == v) break;for(int i = head2[now.pos]; i!=-1; i=E[i].ne) {if(  dis2[E[i].to] > dis2[now.pos] + E[i].w ) {dis2[E[i].to] = dis2[now.pos] + E[i].w;pq.push(point(E[i].to,dis2[E[i].to] ) );	} } } if(dis2[v] == INF) return -1;else return dis2[v];
}
int main()
{char tmp1[1000],tmp2[1000];ll w;ll tmp;//n个点,m条边。 while(~scanf("%d%d",&n,&m) ) {top = 0;cnt1 = 0;cnt2 = 0;memset(head1,-1,sizeof(head1));memset(head2,-1,sizeof(head2));mp.clear();for(int i = 1; i<=m; i++) {scanf("%s %s %lld",tmp1,tmp2,&w);if(mp.find(tmp1) == mp.end() ) mp[tmp1] = ++top;if(mp.find(tmp2) == mp.end() ) mp[tmp2] = ++top;add1(mp[tmp1],mp[tmp2],w);add2(mp[tmp2],mp[tmp1],w);}scanf("%s%s",tmp1,tmp2);if(m == 0) {printf("-1\n");continue;}st = mp[tmp1],ed = mp[tmp2];ll minn = INF;tmp = Dijkstra1(st,ed);if(tmp == -1) {printf("-1\n");continue;}
//		for(int i = 1; i<=n; i++) {
//			printf("%lld  ",dis1[i]);
//		}tmp = Dijkstra2(ed,st);if(tmp == -1) {printf("-1\n");continue;}
//		printf("\n************\n");
//		for(int i = 1; i<=n; i++) {
//			printf("%lld  ",dis2[i]);
//		}
//		printf("\n");for(int i=0;i<m;i++){int u=e[i].pos;int v=e[i].to;ll w=e[i].w;	if(minn>dis1[u]+dis2[v]+(w/2))minn=dis1[u]+dis2[v]+(w/2);
//		   		cout<<u<<"  "<<v<<"  "<<w<<"  "<<minn<<endl;}
//		}printf("%lld\n",minn);}return 0 ;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441926.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php能不能用MyBatis,Mybatis与Ibatis的区别

Mybatis与Ibatis的区别:1、Mybatis实现了接口绑定&#xff0c;使用更加方便在ibatis2.x中我们需要在DAO的实现类中指定具体对应哪个xml映射文件&#xff0c;而Mybatis实现了DAO接口与xml映射文件的绑定&#xff0c;自动为我们生成接口的具体实现&#xff0c;使用起来变得更加省…

【51Nod - 1001 】 数组中和等于K的数对 (排序+ 尺取)

题干&#xff1a; 给出一个整数K和一个无序数组A&#xff0c;A的元素为N个互不相同的整数&#xff0c;找出数组A中所有和等于K的数对。例如K 8&#xff0c;数组A&#xff1a;{-1,6,5,3,4,2,9,0,8}&#xff0c;所有和等于8的数对包括(-1,9)&#xff0c;(0,8)&#xff0c;(2,6)…

linux php oauth安装,Linux php 扩展安装 mongo ,redis ,soap,imap,pdo_mysql,oauth

安装mongodb 参看文章&#xff1a;2.安装redisyum install gitgit clone git://github.com/owlient/phprediscd phpredis/usr/local/php/bin/phpize./configure --with-php-config/usr/local/php/bin/php-configmake && make install如果上述出现报错&#xff0c;可以尝…

*【CF#510C】Fox And Names (拓扑排序)

题干&#xff1a; Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order. After checking some examples, she…

java质,JAVA分解质因子 - osc_r1gtal48的个人空间 - OSCHINA - 中文开源技术交流社区

/*题目分解质因数(5分)题目内容&#xff1a;每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式&#xff0c;这几个素数就都叫做这个合数的质因数。比如&#xff0c;6可以被分解为2x3&#xff0c;而24可以被分解为2x2x2x3。现在&#xff0c;你的程序要读入一个[2,100…

【HDU - 5605】 geometry(水,数学题,推公式)

题干&#xff1a; There is a point PP at coordinate (x,y)(x,y). A line goes through the point, and intersects with the postive part of X,YX,Yaxes at point A,BA,B. Please calculate the minimum possible value of |PA|∗|PB||PA|∗|PB|. Input the first line…

matlab如何画函数的外包络曲线,怎样在MATLAB中划出一个函数的包络线?

沧海一幻觉下面是一系列关于MATLAB的包络线的程序&#xff1a;%这是定义了一个函数&#xff1a;function [up,down] envelope(x,y,interpMethod)%ENVELOPE gets the data of upper and down envelope of the known input (x,y).%% Input parameters:% x the abscissa of the g…

【51nod - 1087】 1 10 100 1000(找规律推公式,水,map)

题干&#xff1a; 1,10,100,1000...组成序列1101001000...&#xff0c;求这个序列的第N位是0还是1。 Input 第1行&#xff1a;一个数T&#xff0c;表示后面用作输入测试的数的数量。&#xff08;1 < T < 10000) 第2 - T 1行&#xff1a;每行1个数N。&#xff08;1 &…

php 异常错误信息用处,关于PHP中异常错误的处理详细介绍

1. 错误报告级别 error_reporting()error_reporting(int $level);PHP 5.4 及以上 E_ALL 包含了 E_STRICT。PHP Manual 所有的错误级别。范例&#xff1a;<?php // 关闭所有PHP错误报告error_reporting(0);// Report simple running errorserror_reporting(E_ERROR | E_WARN…

matlab计算流函数,hanyeah

上面的网址不知道什么时候就打不开了&#xff0c;赶紧保存一份&#xff0c;要不想看都看不到了。什么是流函数&#xff0c;什么是位函数(势函数)&#xff0c;可以自己搜索。说说我这里的应用场景。空间放一些电荷&#xff0c;我们能够算出任意一点的电场强度——一个矢量&#…

【51Nod - 1279】 扔盘子(思维)(on-p会超时)

题干&#xff1a; 有一口井&#xff0c;井的高度为N&#xff0c;每隔1个单位它的宽度有变化。现在从井口往下面扔圆盘&#xff0c;如果圆盘的宽度大于井在某个高度的宽度&#xff0c;则圆盘被卡住&#xff08;恰好等于的话会下去&#xff09;。 盘子有几种命运&#xff1a;1、…

java 内部类私有成员 能访问,为什么外部Java类可以访问内部类私有成员?

HUX布斯如果您想隐藏内部类的私有成员&#xff0c;您可以与公共成员定义一个接口&#xff0c;并创建一个实现此接口的匿名内部类。下面的例子&#xff1a;class ABC{private interface MyInterface{void printInt();}private static MyInterface mMember new MyInterface(){pr…

【HDU - 6290】 奢侈的旅行 (对题目预处理 + DIjkstra最短路)

题干&#xff1a; 高玩小Q不仅喜欢玩寻宝游戏&#xff0c;还喜欢一款升级养成类游戏。在这个游戏的世界地图中一共有nn个城镇&#xff0c;编号依次为11到nn。 这些城镇之间有mm条单向道路&#xff0c;第ii 条单项道路包含四个参数ui,vi,ai,biui,vi,ai,bi&#xff0c;表示一条…

数学建模matlab推荐,推荐数学建模matlab方法整理 - 图文

disp()函数的常见用法1、显示字符串 >> disp(sqrt(2)) sqrt(2)将要显示的字符串必须放在单引号里面&#xff01;&#xff01;&#xff01; 2、显示结果 >> disp(sqrt(2)) 1.41423、显示多个字符>> disp([sqrt(2),num2str(sqrt(2))]) sqrt(2)1.4142格式必须如…

【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护)

题干&#xff1a; There is an apple tree outside of kakas house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree. The tree has N forks which are connected by branches. …

gjr garch Matlab,基于Copula-ARIMA-GJR-GARCH模型的股票指数相关性分析

摘要&#xff1a;当资产收益率分布较为复杂时,研究它们之间的相关关系就变得较为困难。特别是股票市场中资产的收益率分布非正态时,我们几乎不可能去精确模拟几种资产收益率之间的联合分布函数。然而Copula函数恰恰可以解决这类问题。Copula函数是用来描述随机变量间相依结构的…

【HDU - 3974】 Assign the task (dfs序 + 线段树维护 区间更新+ 单点查询)

题干&#xff1a; There is a company that has N employees(numbered from 1 to N),every employee in the company has a immediate boss (except for the leader of whole company).If you are the immediate boss of someone,that person is your subordinate, and all hi…

php 插件 代码架构,php反射机制详以及插件架构实例详解

1。用途&#xff1a;该扩展分析php程序&#xff0c;导出或提取出关于类、方法、属性、参数等的详细信息&#xff0c;包括注释。Reflection可以说是对php库函数&#xff1a;“Classes/Objects 类&#xff0f;对象函数”的一个扩展。主要用在通过程序检测现有php程序内部关于类、…

【HDU - 1540】 Tunnel Warfare (线段树进阶操作 区间合并+ 单点更新+ 最长覆盖区间查询 )

题干&#xff1a; During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was …

php扩展包安装了为啥没加载,已安装PHP扩展但未加载

我正在尝试安装php的ssh2扩展,并且有一点点困难.文件在那里,它只是没有加载到PHP.首先,我安装了ssh2&#xff1a;aptitude install libssh2-1-dev libssh2-php(对于它的价值,我在Nginx上运行Ubuntu 12.04.)我可以看到使用modules命令加载ssh2&#xff1a;php -m |grep ssh2ssh2…