// 题意 : 一个人要去旅行 给你起点和终点 求最少花费  其中花费为经过路径的总费用减去该路径的中的最大花费段
// 直接搜索 稍微加了个剪枝 主要是数据规模小
#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的更多相关文章

  1. Travelling Fee(Dijlstra——最短路问题变型)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2027 题目: Samball is going to trav ...

  2. ZOJ1027 Travelling Fee(DP+SPFA)

    给一张有向无环图,边都有花费,从某点到某点走的那条路径上的那一条花费最多的边可以省掉,问从起点到终点的最少花费的多少, 往DP想的话,就可以写出这个状态dp[u][mx],表示到达u点已经省掉的花费为 ...

  3. 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 / ...

  4. Travelling

    Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. ZOJ题目分类

    ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...

  6. HDU-3001 Travelling

    http://acm.hdu.edu.cn/showproblem.php?pid=3001 从任何一个点出发,去到达所有的点,但每个点只能到达2次,使用的经费最小.三进制 Travelling Ti ...

  7. hdu 3001 Travelling (TSP问题 )

    Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. hdu 3001 Travelling(状态压缩 三进制)

    Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. Travelling(spfa+状态压缩dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 Travelling Time Limit: 6000/3000 MS (Java/Others ...

随机推荐

  1. 一些css效果积累

    悬浮效果: ul li a{text-decoration: none;color: black}  ul li a:hover{color: blue}   鼠标变小手 span:hover{cur ...

  2. 获取c++ edit控件内容

    CString str1,str2; ((CEdit*)GetDlgItem(IDC_EDIT1))->GetWindowText(str1); ((CEdit*)GetDlgItem(IDC_ ...

  3. [转载]C# 多线程、控制线程数提高循环输出效率

    C#多线程及控制线程数量,对for循环输出效率. 虽然输出不规律,但是效率明显提高. 思路: 如果要删除1000条数据,只使用for循环,则一个接着一个输出.所以,把1000条数据分成seed段,每段 ...

  4. PHP之Error与Logging函数讲解

    PHP Error 和 Logging 简介 error 和 logging 函数允许你对错误进行处理和记录. error 函数允许用户定义错误处理规则,并修改记录错误的方式. logging 函数允 ...

  5. POJ 2080 Calendar(很水的模拟)

    刚开始一直WA,才发现原来代码中两处减去年份.月份的天数的判断条件用的是>=,虽然最后考虑n=0要退回一天的情况,但还是WA.后来改成>的条件判断,省去了考虑n=0的麻烦,AC. 此题无非 ...

  6. Relevance Between Variable Declaration and Definition in C++

    A declaration makes a name known to a programm. A definition creates the assocatied entity. A variab ...

  7. UIkit的confirm,好看点

    一,官方推荐的样码. <button type="button" class="uk-button" onclick="UIkit.modal. ...

  8. selenium2 安装、简单使用及浏览器启动问题解决汇总

    一.selenium2 安装 1.安装jdk并配置环境变量 jdk需要1.6版本及以上的,这个从网上下载就可以,环境变量的配置前边的随笔整理过就不多说了. 2.安装Firefox,Selenium I ...

  9. hihocoder 1084 扩展KMP && 2014 北京邀请赛 Justice String

    hihocoder 1084 : http://hihocoder.com/problemset/problem/1084 北京邀请赛 Just  String http://www.bnuoj.co ...

  10. python url编码,解码

    >>> urllib.unquote('%E4%B8%BD%E6%B1%9F') >>> data '\xe4\xb8\xbd\xe6\xb1\x9f' >& ...