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 ...
随机推荐
- 将Windows上的文件上传到Linux上
下载一个SSH Secure Shell Client即可. SSHSecureShellClient-3.2.9下载地址: 免费下载地址在 http://linux.linuxidc.com/ 用户 ...
- hdu 2940
简单的大数乘法,直接改16进制~~ #include <cstdio> #include <cstdlib> #include <cmath> #include & ...
- 使用shell脚本获取虚拟机中cpu使用率(读/proc/statc)
#!/bin/bash interval= cpu_num=`-] -c` start_idle=() start_total=() cpu_rate=() cpu_rate_file=./`host ...
- Unique Binary Search Tree II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- go语言入门
Go语言最主要的特性: 自动垃圾回收 更丰富的内置类型 函数多返回值 错误处理 匿名函数和闭包 类型和接口 并发编程 反射 语言交互性 1.2.4 错误处理Go语言引入了3个关键字用 ...
- TCL语言笔记:TCL中的列表操作
一.介绍 列表则是具有特殊解释的字符串.Tcl 中的列表操作和其它 Tcl 命令一样具有相同的结构.列表可应用在诸如 foreach 这样的以列表为变元的循环命令中,也应于构建 eval 命令的延迟命 ...
- 手动安装UniDAC的过程
每次安装都不一样,而且有时候挺麻烦的,所以记下来.我这次安装过程是这样的: 0. 修改Source\Delphi7\Make.bat,填入正确的IdeDir 1.找到source\delphi7目录, ...
- QT里使用sqlite的问题,好多坑
1. 我使用sqlite,开发机上好好的,测试机上却不行.后来发现是缺少驱动(Driver not loaded Driver not loaded),代码检查了又检查,发现应该是缺少dll文件(系统 ...
- button 事件属性
- 31. Next Permutation
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...