hdu 3268 09 宁波 现场 I - Columbus’s bargain 读题 最短路 难度:1
Description
Because the ships are not large enough and there are seldom harbors in his route, Columbus had to buy food and other necessary things from savages. Gold coins were the most popular currency in the world at that time and savages also accept them. Columbus wanted to buy N kinds of goods from savages, and each kind of goods has a price in gold coins. Columbus brought enough glass beads with him, because he knew that for savages, a glass bead is as valuable as a gold coin. Columbus could buy an item he need only in four ways below:
1. Pay the price all by gold coins.
2. Pay by ONE glass bead and some gold coins. In this way, if an item’s price is k gold coins, Columbus could just pay k � 1 gold coins and one glass bead.
3. Pay by an item which has the same price.
4. Pay by a cheaper item and some gold coins.
Columbus found out an interesting thing in the trade rule of savages: For some kinds of goods, when the buyer wanted to buy an item by paying a cheaper item and some gold coins, he didn’t have to pay the price difference, he can pay less. If one could buy an item of kind A by paying a cheaper item of kind B plus some gold coins less than the price difference between B and A, Columbus called that there was a “bargain” between kind B and kind A. To get an item, Columbus didn’t have to spend gold coins as many as its price because he could use glass beads or took full advantages of “bargains”. So Columbus wanted to know, for any kind of goods, at least how many gold coins he had to spend in order to get one � Columbus called it “actual price” of that kind of goods.
Just for curiosity, Columbus also wanted to know, how many kinds of goods are there whose “actual price” was equal to the sum of “actual price” of other two kinds.
Input
The first line in the input is an integer T indicating the number of test cases ( 0 < T <= 10).
For each test case:
The first line contains an integer N, meaning there are N kinds of goods ( 0 < N <= 20). These N kinds are numbered from 1 to N.
Then N lines follow, each contains two integers Q and P, meaning that the price of the goods of kind Q is P. ( 0 <Q <=N, 0 < P <= 30 )
The next line is a integer M( 0 < M <= 20 ), meaning there are M “bargains”.
Then M lines follow, each contains three integers N1, N2 and R, meaning that you can get an item of kind N2 by paying an item of kind N1 plus R gold coins. It’s guaranteed that the goods of kind N1 is cheaper than the goods of kind N2 and R is none negative and less than the price difference between the goods of kind N2 and kind N1. Please note that R could be zero.
Output
Please output N lines at first. Each line contains two integers n and p, meaning that the “actual price” of the goods of kind n is p gold coins. These N lines should be in the ascending order of kind No. .
Then output a line containing an integer m, indicating that there are m kinds of goods whose “actual price” is equal to the sum of “actual price” of other two kinds.
Sample Input
4
1 4
2 9
3 5
4 13
2
1 2 3
3 4 6
Sample Output
2 6
3 4
4 10
1
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxm=50001;
const int maxn=51;
struct edge{
int to,cost,nxt;
}e[maxm];
int first[maxn],price[maxn],m,n,len;
void addedge(int f,int t,int c){
e[len].to=t;e[len].cost=c;e[len].nxt=first[f];
first[f]=len++;
}
void debug(){
for(int i=1;i<=n;i++){
for(int p=first[i];p!=-1;p=e[p].nxt){
int t=e[p].to;
printf("f %d t %d c %d\n",i,t,e[p].cost);
}
}
}
queue<int>que;
void spfa(){
for(int i=1;i<=n;i++)que.push(i);
while(!que.empty()){
int f=que.front();que.pop();
for(int p=first[f];p!=-1;p=e[p].nxt){
int t=e[p].to;
if(price[t]>price[f]+e[p].cost){
price[t]=price[f]+e[p].cost;
que.push(t);
}
}
}
}
int main(){
int caT;
scanf("%d",&caT);
while(caT--){
memset(first,-1,sizeof(first));
memset(price,0x3f,sizeof(price));
scanf("%d",&n);
int f,t,c;
len=0;
for(int i=0;i<n;i++){scanf("%d%d",&f,&c);price[f]=c;}
scanf("%d",&m);
for(int i=0;i<m;i++){scanf("%d%d%d",&f,&t,&c);if(f!=t)addedge(f,t,c);}
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(i!=j&&price[i]==price[j]){addedge(j,i,price[i]-price[j]);addedge(i,j,price[i]-price[j]);}//添加第三种,即等价交换情况边
for(int i=1;i<=n;i++)if(price[i]>0)price[i]--;//直接把玻璃珠在初始值里减去
//debug();
spfa();
for(int i=1;i<=n;i++)printf("%d %d\n",i,price[i]);
int ans=0;
for(int i=1;i<=n;i++){
bool fl=false;
for(int j=1;j<=n;j++){
for(int k=j+1;k<=n;k++){
if(i!=j&&i!=k&&price[j]+price[k]==price[i]){fl=true;break;}
}
if(fl)break;
}
if(fl)ans++;
}
printf("%d\n",ans);
}
return 0;
}
hdu 3268 09 宁波 现场 I - Columbus’s bargain 读题 最短路 难度:1的更多相关文章
- hdu 3262 09 宁波 现场 C - Seat taking up is tough 暴力 难度:0
Description Students often have problems taking up seats. When two students want the same seat, a qu ...
- hdu 3264 09 宁波 现场 E - Open-air shopping malls 计算几何 二分 圆相交面积 难度:1
Description The city of M is a famous shopping city and its open-air shopping malls are extremely at ...
- hdu 3687 10 杭州 现场 H - National Day Parade 水题 难度:0
H - National Day Parade Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- hdu 3682 10 杭州 现场 C To Be an Dream Architect 容斥 难度:0
C - To Be an Dream Architect Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...
- hdu 4771 13 杭州 现场 B - Stealing Harry Potter's Precious 暴力bfs 难度:0
Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. W ...
- HDU 4572 Bottles Arrangement(找规律,仔细读题)
题目 //找规律,123321123321123321…发现这样排列恰好可以错开 // 其中注意题中数据范围: M是行,N是列,3 <= N < 2×M //则猜测:m,m,m-1,m-1 ...
- hdu 5071(2014鞍山现场赛B题,大模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 思路:模拟题,没啥可说的,移动的时候需要注意top的变化. #include <iostr ...
- Columbus’s bargain
Columbus’s bargain Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 2191 珍惜现在,感恩生活 多重背包入门题
背包九讲下载CSDN 背包九讲内容 多重背包: hdu 2191 珍惜现在,感恩生活 多重背包入门题 使用将多重背包转化为完全背包与01背包求解: 对于w*num>= V这时就是完全背包,完全背 ...
随机推荐
- CRM - 销售与客户
一.销售与客户 - 表结构 ---公共客户(公共资源) 1.没有报名 2.3天没有跟进 3.15天没有成单 客户分布表 龙泰 男 yuan 2018-5-1 3天未跟进 龙泰 男 三江 2018-5- ...
- Struts2表单数据接收方式
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/sunshoupo211/article/details/30249239 1.将Action类作 ...
- 003-and design-在create-react-app项目中使用antd
一.概述 create-react-app 是业界最优秀的 React 应用开发工具之一,本文会尝试在 create-react-app 创建的工程中使用 antd 组件,并自定义 webpack 的 ...
- django之中间件、缓存、信号、admin内置后台
目录: 中间件 缓存 信号 admin后台 一.中间件 1.什么是中间件? 中间件是一个.一个的管道,如果相对任何所有的通过Django的请求进行管理都需要自定义中间件 中间件可以对进来的请求和出去的 ...
- ref out 区别
1.使用ref型参数时,传入的参数必须先被初始化.对out而言,必须在方法中对其完成初始化. 2.使用ref和out时,在方法的参数和执行方法时,都要加Ref或Out关键字.以满足匹配. 3.out适 ...
- boost pool 和 object_pool
内存池(Memory Pool)是一种内存分配方式. 通常我们习惯直接使用new.malloc等API申请分配内存,这样做的缺点在于:由于所申请内存块的大小不定,当频繁使用时会造成大量的 ...
- rails性能优化
1,使用Unicorn或者Thin服务器替代默认的webrick.2,静态资源压缩合并,放到云存储上.3,同时可以使用rails的Turbolinks,使用js替换title和body,但也带来了js ...
- spark环境安装
源码包下载: http://archive.apache.org/dist/spark/spark-2.1.1/v 集群环境: master 192.168.1.99 slave1 192.168.1 ...
- springcloud10---feign-with-hystrix_factory
package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframew ...
- cookie的存取删
存: document.cookie = "name=Kevin;expires="+new Date().getDate()+7; //有效期7天 取: function Get ...