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. Leetcode_num13_Climbing Stairs

    称号: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...

  2. 部署、收回和删除解决方式----STSADM和PowerShell

    部署.收回和删除解决方式----STSADM和PowerShell         由于近期总是要部署wsp解决方式,所以常常要用到命令行或者PowerShell.所以有必要将命令集中放在这里.在部署 ...

  3. Android中G-Sensor相关流程

    1.使G-sensor正常工作需要做的事: G-sensor driver文件包括: driver/i2c/chips/lis331dl.c driver/i2c/chips/sensorioctl. ...

  4. selenium + firefox/chrome/phantomjs登陆之模拟点击

    登陆之模拟点击 工具:python/java + selenium + firefox/chrome/phantomjs (1)windows开发环境搭建 默认已经安装好了firefox 安装pip ...

  5. inkscape - 百度百科

    http://wapbaike.baidu.com/view/1267893.htm?ssid=0&from=844b&uid=3151E6C0905477A13653132D762B ...

  6. SE 2014年5月5日

    如图配置 某企业网络规划图(三台交换设备/三台路由设备) 接入层 SW1 连接终端用户 汇聚层 SW2 SW3 核心层 R1 R2 R5 1. 如图 SW1 SW2 SW3 物理链路两两相连接,网络中 ...

  7. IIS7 配置 PHP5.5

    本文环境: 操作系统:Win7(x64) 中文专业版 PHP       :V5.5 首先添加IIS. 控制面板-〉程序-〉打开或关闭Windows功能 1. 勾选“Internet 信息服务”   ...

  8. Knockout应用开发指南 第三章:绑定语法(3)

    原文:Knockout应用开发指南 第三章:绑定语法(3) 12   value 绑定 目的 value绑定是关联DOM元素的值到view model的属性上.主要是用在表单控件<input&g ...

  9. Windows Phone开发(42):缓动动画

    原文:Windows Phone开发(42):缓动动画 前面在讨论关键帧动画的时候,我有意把几个带缓动动画的关键帧动画忽略掉,如EasingColorKeyFrame.EasingDoubleKeyF ...

  10. HDU1698_Just a Hook(线段树/成段更新)

    解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...