POJ 3835 & HDU 3268 Columbus’s bargain(最短路 Spfa)
题目链接:
POJ:http://poj.org/problem?id=3835
HDU:http://acm.hdu.edu.cn/showproblem.php?pid=3268
thought was India.
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.
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.
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.
1
4
1 4
2 9
3 5
4 13
2
1 2 3
3 4 6
1 3
2 6
3 4
4 10
1
题意:
哥伦布和别人交易,用金币,交易规则:
1、每次买一个东西能够用一个玻璃珠取代1金币。
2、能够用同样的价格物品进行交换
3、假设对方愿意能够用N1+r金币交换到N2物品。
求最后每件物品的最小价格,而且最后输出有多少个物品价格=其它两个物品价格和。
PS:
首先建立一个源点S,然后连接全部的点,边上的权值为最初的价钱p - 1,然后在可用来交易的物品间建边,权值为R,然后价钱相等的物品再建边,权值为 0 ,求以S为源点的单源多点最短路;
代码例如以下:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctype.h>
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define pi acos(-1.0)
#define INF 0xffffff
#define N 1005
#define M 2005
int n,m,k,Edgehead[N],dis[N];
struct
{
int v,w,next;
} Edge[2*M];
bool vis[N];
int vv[N];
int cont[N];
void Addedge(int u,int v,int w)
{
Edge[k].next = Edgehead[u];
Edge[k].w = w;
Edge[k].v = v;
Edgehead[u] = k++;
}
void SPFA( int start)
{
queue<int>Q;
for(int i = 1 ; i <= n ; i++ )
dis[i] = INF;
dis[start] = 0;
++cont[start];
memset(vis,false,sizeof(vis));
Q.push(start);
while(!Q.empty())//直到队列为空
{
int u = Q.front();
Q.pop();
vis[u] = false;
for(int i = Edgehead[u] ; i!=-1 ; i = Edge[i].next)//注意
{
int v = Edge[i].v;
int w = Edge[i].w;
if(dis[v] > dis[u] + w)
{
dis[v] = dis[u]+w;
if( !vis[v] )//防止出现环,也就是进队列反复了
{
Q.push(v);
vis[v] = true;
}
// if(++cont[v] > n)//有负环
// return -1;
}
}
}
//return dis[n];
}
int main()
{
int t;
int u,v,w;
int n1, n2, r;
int a, p; scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
//scanf("%d%d",&m,&n);//n为目的地
k = 1;
memset(Edgehead,-1,sizeof(Edgehead));
memset(vv,0,sizeof(vv));
for(int i = 1; i <= n; i++)
{
scanf("%d%d",&a,&p);
vv[a] = p;
Addedge(0,a,p-1);
}
scanf("%d",&m);
for(int i = 1 ; i <= m ; i++ )
{
scanf("%d%d%d",&u,&v,&w);
Addedge(u,v,w);
//Addedge(v,u,w);//双向链表
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(i!=j && vv[i]==vv[j])//价钱相等
{
Addedge(i,j,0);//价钱相等权值就是零
Addedge(j,i,0);
}
}
}
SPFA(0);//从点0開始寻找最短路
for(int i = 1; i <= n; i++)
{
printf("%d %d\n",i,dis[i]);
}
memset(vis,0,sizeof(vis));
int num = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
// if(i != j)
// {
for(int k = j+1; k <= n; k++)
{
if(i != j)
{
if(!vis[i] && i!=k && dis[i]==dis[j]+dis[k])
{
vis[i] = true;
num++;
}
}
}
}
// }
}
printf("%d\n",num);
}
return 0;
}
POJ 3835 & HDU 3268 Columbus’s bargain(最短路 Spfa)的更多相关文章
- HDU 1535 Invitation Cards(最短路 spfa)
题目链接: 传送门 Invitation Cards Time Limit: 5000MS Memory Limit: 32768 K Description In the age of te ...
- POJ 2387 Til the Cows Come Home 【最短路SPFA】
Til the Cows Come Home Description Bessie is out in the field and wants to get back to the barn to g ...
- Columbus’s bargain
Columbus’s bargain Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- (poj)3268 Silver Cow Party 最短路
Description One cow ≤ N ≤ ) conveniently numbered ..N ≤ X ≤ N). A total of M ( ≤ M ≤ ,) unidirection ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- hdu 1874 畅通工程续(模板题 spfa floyd)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1874 spfa 模板 #include<iostream> #include<stdio ...
- HDU 3268/POJ 3835 Columbus’s bargain(最短路径+暴力枚举)(2009 Asia Ningbo Regional)
Description On the evening of 3 August 1492, Christopher Columbus departed from Palos de la Frontera ...
- hdu 3268 09 宁波 现场 I - Columbus’s bargain 读题 最短路 难度:1
Description On the evening of 3 August 1492, Christopher Columbus departed from Palos de la Frontera ...
- POJ 3831 & HDU 3264 Open-air shopping malls(几何)
题目链接: POJ:id=3831" target="_blank">http://poj.org/problem?id=3831 HDU:http://acm.h ...
随机推荐
- jQuery(expression, [context]) , $(即jQuery)的參数问题
jQuery(expression, [context]) 返回值:jQuery 概述 这个函数接收一个包括 CSS 选择器的字符串,然后用这个字符串去匹配一组元素. jQuery 的 ...
- c,const和指针组合的几种意义
const和指针的组合: 注释部分表示非法. ; ; p=&a; //*p = 1 ; ; pp= &a; //*pp = 1; ; //pv=&a; *pv = ; ; // ...
- 网页制作之JavaScript部分 1 - 语法(复制教材内容)
一.简介 1.JavaScript它是个什么东西? 它是个脚本语言,需要有宿主文件,他的宿主文件是html文件. 2.它与Java有什么关系? 没有什么直接联系,java是Sun公司(已经没有了,被O ...
- 【转】android加载大量图片内存溢出的三种解决办法
方法一: 在从网络或本地加载图片的时候,只加载缩略图. /** * 按照路径加载图片 * @param path 图片资源的存放路径 * @param scalSize 缩小的倍数 * @return ...
- 演练5-5:Contoso大学校园管理系统5
Contoso University示例网站演示如何使用Entity Framework 5创建ASP.NET MVC 4应用程序. Entity Framework有三种处理数据的方式: Data ...
- Ubuntu下ssh免password登录安装
1.首先在本机安装openssh-server和openssh-client. 命令:sudo apt-get install openssh-server openssh-client 2.在检查当 ...
- UML03-类图
1.在类图中,聚合关系表达总体与局部的关系. 2.请根据下面的需求,画出用例图和类图. 系统允许管理员通过磁盘加载存货数据来运行存货清单报告: 管理员通过从磁盘加载存货数据.向磁盘保存存货数据来更新存 ...
- 初入Android--Activate生命周期
Activate的主要生命周期 (注意:这只是主要的生命周期,而不是完整的生命周期方法,其中的两个周期之间可能还执行了其他的一些方法) 每个时刻在屏幕上的状态 进入onCreate方法:Activat ...
- 讲解下for循环的用法,加深记忆
引子 这是一段很简单的代码,但是即便是这么简单的东西,这里我们还是需要说一下. 关于for循环整个执行流程就是,先执行var i=10,然后到了第二个语句,判断10是否大于0,很明显为true,所以此 ...
- QTableWidget表格合并若干问题及解决方法
Qt提供 QTableWidget作为表格的类以实现表格的基本功能,表格中所装载的每一个单元格由类QTableWidgetItem提供.这是基于表格实现 Qt提供的一个基础类,若想实现定制表格和单元格 ...