zoj 2027 Travelling Fee
// 题意 : 一个人要去旅行 给你起点和终点 求最少花费 其中花费为经过路径的总费用减去该路径的中的最大花费段
// 直接搜索 稍微加了个剪枝 主要是数据规模小
#include <iostream>
#include <map>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define MOD 1000000007
#define maxn
map<string,int> mp;
vector <int >V[];
bool visit[];
int rc[][];
char str1[],str2[];
char ss[],ts[];
int s,t;
int ans;
void dfs(int u,int sum,int mx){
// if(u==t) return;
visit[u]=true;
if(sum-mx>ans) return; // 加了个小剪枝
int i,v,ts,tm;
int len=V[u].size();
for(i=;i<len;i++){
v=V[u][i];
if(!visit[v]){
ts=sum+rc[u][v];
tm=max(mx,rc[u][v]);
if(v==t){
ans=min(ans,ts-tm);
return ;
}
dfs(v,ts,tm);
visit[v]=false;
}
} }
int main(){
int m;
int n;
while(scanf("%s %s",ss,ts)!=EOF){
scanf("%d",&m);
int i,val;
int u,v;
n=;
map<string,int>::iterator it;
for(i=;i<m;i++){
scanf("%s %s %d",str1,str2,&val);
it=mp.find(str1);
if(it!=mp.end()){
u= mp[str1];
}
else{
u=n++;
mp[str1]=u;
}
it=mp.find(str2);
if(it!=mp.end()){
v= mp[str2];
}
else{
v=n++;
mp[str2]=v;
}
V[u].push_back(v);
rc[u][v]=val;
}
s=mp[ss];
t=mp[ts];
memset(visit,,sizeof(visit));
ans=MOD;
//printf("%d %d\n",s,t);
dfs(s,,);
printf("%d\n",ans);
mp.clear();
for(i=;i<n;i++)
V[i].clear();
}
}
zoj 2027 Travelling Fee的更多相关文章
- Travelling Fee(Dijlstra——最短路问题变型)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2027 题目: Samball is going to trav ...
- ZOJ1027 Travelling Fee(DP+SPFA)
给一张有向无环图,边都有花费,从某点到某点走的那条路径上的那一条花费最多的边可以省掉,问从起点到终点的最少花费的多少, 往DP想的话,就可以写出这个状态dp[u][mx],表示到达u点已经省掉的花费为 ...
- POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...
- Travelling
Travelling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- ZOJ题目分类
ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...
- HDU-3001 Travelling
http://acm.hdu.edu.cn/showproblem.php?pid=3001 从任何一个点出发,去到达所有的点,但每个点只能到达2次,使用的经费最小.三进制 Travelling Ti ...
- hdu 3001 Travelling (TSP问题 )
Travelling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 3001 Travelling(状态压缩 三进制)
Travelling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Travelling(spfa+状态压缩dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 Travelling Time Limit: 6000/3000 MS (Java/Others ...
随机推荐
- POJ2104 K-th Number Range Tree
又是区间第k大,这次选择这道题是为以后写线段树套平衡树铺路的.Range Tree可以理解成线段树套vector吧,相当于每个结点多存了对应区间的一个排好序的序列.画一下就会知道空间的消耗是nlogn ...
- java数据结构学习(一)之二分查找
二分查找法与我们在孩童时期玩过的猜数的游戏一样,这个游戏里一个朋友会让你猜他正想的一个1至100的数,当你猜了一个数后,他会告诉你三种选择的一个:你猜的比她想的大,或小,或猜中了.为了能用最少的次 ...
- ubuntu -LDAP的配置
本文内容来自 http://blog.csdn.net/jl19861101/article/details/5582841 1. LDAP Server1.1. 安装主要安装一下套件: 代码: # ...
- U盘安装Win7 64位
试了好几遍,失败了的就不说了,直接记下成功的方案,方便下次. 方法为:用UltraISO刻镜像文件到U盘,然后U盘启动安装. 具体如下: 刻u盘之前一定要验证iso镜像的完整性啊(可以用文件校验工具与 ...
- 关于in与exists的效率讨论
关于in与exists的效率讨论1).select * from A where id in (select id from B)以上查询使用了in语句,in只执行一次,他查出B表的所有id字段并缓存 ...
- Project Euler 89:Roman numerals 罗马数字
Roman numerals For a number written in Roman numerals to be considered valid there are basic rules w ...
- *[topcoder]LCMSetEasy
http://community.topcoder.com/stat?c=problem_statement&pm=13040 DFS集合全排列+LCM和GCD.但事实上,有更简单的算法,列在 ...
- 我30天在Stack Overflow问答网站上回答问题的感受
想法的萌芽 如果非要总结下我多年来是如何使用Stack Overflow的话,我的答案就是:打开网页,搜索问题,查看Stack Overflow的搜索结果,参考答案,最后再关掉网页. 我的生活已经离不 ...
- HTML5入门九---Canvas画布
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 天气API整理,返回的数据格式为json对象
中国天气的API惨遭封杀,不得已更换其他的API使用吧. 这里大部分API来自APP的数据包截取或者反汇编,所以各开发使用者不得用于商业用途,否则后果自负! 1.金山网址导航 来源:http://ww ...