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. Xsoup 是一款基于 Jsoup 开发的

    Xsoup 是一款基于Jsoup 开发的,使用XPath抽取Html元素的工具.它被用于作者的爬虫框架 WebMagic 中,进行XPath 解析和抽取. 此次更新主要增加了一些XPath语法的支持. ...

  2. linux zombie process相关学习

    1. zombie process是什么? zombie process是那些在系统中已经死掉的process, 通过ps -A | grep defunct可以查看系统中有多少zombie proc ...

  3. no copy constructor available or copy constructor is declared &#39;explicit&#39;

    今天新写了一个类.然后对这个类使用STL中的vector,碰到错误: no copy constructor available or copy constructor is declared 'ex ...

  4. C++ 中获取 可变形參函数中的參数

    #include <iostream> #include <stdarg.h> using namespace std; int ArgFunc(const char * st ...

  5. 在ASP.NET MVC 中获取当前URL、controller、action(转)

    URL的获取很简单,ASP.NET通用: [1]获取 完整url (协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [2]获取 虚拟目录 ...

  6. hdu1159 LCS模板题

    题目分析 pid=1159">原题地址 最简单的最长公共子序列(LCS)问题的模板题了.不解释. ------------------------------------------- ...

  7. Jetty:开发指导Handlers

    Rewrite Handler RewriteHandler匹配一个基于该请求的规则集合,然后根据匹配规则的变更请求. 最常见的要求是改写URI.但不限于:规则可以被配置为重定向响应.设置cookie ...

  8. Learning Cocos2d-x for WP8(5)——详解Menu菜单

    原文:Learning Cocos2d-x for WP8(5)--详解Menu菜单 C#(wp7)兄弟篇Learning Cocos2d-x for XNA(5)——详解Menu菜单 菜单是游戏必不 ...

  9. CF 460C Present 【DP+】主意

    给你n高树花.m日,每天连续浇筑w鲜花.一天一次,花长1高度单位 求m天后.最矮的花最高是多少 最大最小问题能够用二分来解 首先我们能够得到全部花的最矮高度即答案的下界,给这个花浇m天即是答案的上界 ...

  10. SQLiteDatabase和Contentprovider

    SQLiteDatabase和Contentprovider这两个数据库,我一般是用前面一个,喜欢它的操作数据库的语句,简单明了,可惜有时遇到数据库同步的问题,有时我们需要在一个数据库下建立多个表,多 ...