UVA 538 - Balancing Bank Accounts

题目链接

题意:给定一些人的欠钱关系,要求在n-1次内还清钱,问方案

思路:贪心,处理出每一个人最后钱的状态,然后直接每一个人都和最后一个人操作就可以

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <map>
using namespace std; const int N = 25; int n, m, have[N];
map<string, int> hash;
string name[N]; int main() {
int cas = 0;
while (~scanf("%d%d", &n, &m) && n || m) {
hash.clear();
memset(have, 0, sizeof(have));
for (int i = 1; i <= n; i++) {
cin >> name[i];
hash[name[i]] = i;
}
string a, b; int val;
while (m--) {
cin >> a >> b >> val;
int u = hash[a], v = hash[b];
have[u] += val;
have[v] -= val;
}
printf("Case #%d\n", ++cas);
for (int i = 1; i < n; i++) {
if (have[i] < 0)
cout << name[i] << " " << name[n] << " " << -have[i] << endl;
else if (have[i] > 0)
cout << name[n] << " " << name[i] << " " << have[i] << endl;
have[n] -= have[i];
}
printf("\n");
}
return 0;
}

UVA 538 - Balancing Bank Accounts(贪心)的更多相关文章

  1. 【NOIP合并果子】uva 10954 add all【贪心】——yhx

    Yup!! The problem name reects your task; just add a set of numbers. But you may feel yourselvesconde ...

  2. uva 11134 fabled rooks (贪心)——yhx

    We would like to place n rooks, 1 n 5000, on a n nboard subject to the following restrictions• The i ...

  3. UVa 11134 (区间上的贪心) Fabled Rooks

    这道题真是WA得我心力交瘁,好讨厌的感觉啊! 简直木有写题解的心情了 题意: n×n的棋盘里,放置n个车,使得任意两车不同行且不同列,且第i个车必须放在给定的第i个矩形范围内.输出一种方案,即每个车的 ...

  4. UVA 12382 Grid of Lamps 贪心

    题目链接: C - Grid of Lamps Time Limit:1000MSMemory Limit: 0KB 问题描述 We have a grid of lamps. Some of the ...

  5. uva 993 Product of digits (贪心 + 分解因子)

      Product of digits  For a given non-negative integer number N , find the minimal natural Q such tha ...

  6. UVA 11729 - Commando War(贪心 相邻交换法)

    Commando War There is a war and it doesn't look very promising for your country. Now it's time to ac ...

  7. uva 714 - Copying Books(贪心 最大值最小化 二分)

    题目描写叙述开头一大堆屁话,我还细致看了半天..事实上就最后2句管用.意思就是给出n本书然后要分成k份,每份总页数的最大值要最小.问你分配方案,假设最小值同样情况下有多种分配方案,输出前面份数小的,就 ...

  8. UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】

    UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...

  9. UVA - 11636 Hello World! (贪心)

    思路:复制次数最少并且可以部分复制,那么贪心地让当前尽量多的复制,如果最后一次复制会超过n,那就部分复制.即满足并且x尽量小. AC代码 #include <stdio.h> const ...

随机推荐

  1. 在java代码中进行px与dip(dp)、px与sp单位值的转换

        其实都是以前保存的代码,最近发现自己的资料库很混乱,索性都整理成博客,方便以后自己要用的时候快速找到. DisplayUtil.java /** * 单位转换工具 * * @author ca ...

  2. vim配置(vimplus)

    vim配置(vimplus) vimplus vimplus是vim的超级配置安装程序 github地址:https://github.com/chxuan/vimplus.git,欢迎star和fo ...

  3. hdu4352(数位dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4352 题意:求区间L到R之间的数A满足A的的数位的最长递增序列的长度为K的数的个数. 分析:数位dp, ...

  4. 金句: "對比MBA學位,我們更需要PSD學位的人!" Poor, Smart and Deep Desire to… | consilient_lollapalooza on Xanga

    金句: "對比MBA學位,我們更需要PSD學位的人!" Poor, Smart and Deep Desire to… | consilient_lollapalooza on X ...

  5. xcode project

    An Xcode project is a repository for all the files, resources, and information required to build one ...

  6. embedded dylibs/frameworks are only supported on iOS 8.0 and later 错误解决

    ld: warning: embedded dylibs/frameworks only run on iOS 8 or later ld: embedded dylibs/frameworks ar ...

  7. Codeforces Round #277.5 (Div. 2)---B. BerSU Ball (贪心)

    BerSU Ball time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  8. matlab 2014a 改为英文版本号

    1. 在 Matlab 的安装目录以下找到例如以下的路径,X:\MATLAB\R2014a\java\jar,当中 X 为安装盘符,这个不用过多解释了,然后找到目录 zh_CN.此目录就是中文界面的语 ...

  9. Android四个存储数据的SharedPreferences

    除了SQLite外部数据库.SharedPreferences它是一个轻量级的数据存储,其本质是基于XML文件存储key-value键值数据,通过定期用它来存储一些简单的配置信息.它的存储位置/dat ...

  10. 利用Sambaserver在Ubuntu系统和Win7系统间共享目录

    1 介绍 如今是网络化的时代,我们每一个人要更好的发展.离不开网络化.信息化的支持.利用网络的支持.在不同的操作系统间共享文件等信息,是计算机专业学生必备的一项技能. 本文所讲的就是怎样建立.设置.链 ...