http://poj.org/problem?id=2240

用log化乘法为加法找正圈

c++ 110ms,g++tle

#include <string>
#include <map>
#include <iostream>
#include <cmath>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 50;
bool vis[maxn];
double chg[maxn][maxn];
double dis[maxn];
int e[maxn][maxn],deg[maxn];
map<string,int> idmp;
int n,m;
const double inf = 0x3fffffff; queue<int> que;
bool hasloop(int s){
while(!que.empty())que.pop();
que.push(s);
vis[s] = true;
int cnt = 0;
while(!que.empty()){
cnt ++;
s = que.front();que.pop();vis[s] = false;
for(int i = 0;i < deg[s];i++)
{
int t = e[s][i];
if(dis[t] < dis[s] + chg[s][t])
{
dis[t] = dis[s] + chg[s][t];
que.push(t);
vis[t] = true;
}
}
if(cnt > n * n)return true;
}
return false;
} int main(){
int ti = 0;
while(cin>>n && n && ++ti){
idmp.clear();
for(int i = 0;i < n;i++)
{
dis[i] = -inf;
for(int j = 0;j < n;j++)chg[i][j] = -inf;
}
memset(vis,false,sizeof vis);
memset(deg,0,sizeof deg); for(int i = 0;i < n;i++)
{
string tmp;
cin>>tmp;
idmp[tmp] = i;
}
cin>>m;
for(int i = 0;i < m;i++)
{
string sf,st;
double change;
cin>>sf>>change>>st;
change = log(change);
int f = idmp[sf];
int t = idmp[st];
chg[f][t] = change;
e[f][deg[f]++] = t;
}
bool fl = false;
for(int i = 0;i < n;i++)
{
if(dis[i] == -inf){
dis[i] = 1;
if(hasloop(i)){
fl = true;
break;
}
}
}
cout << "Case " << ti << ": ";
if(fl)cout << "Yes" <<endl;
else cout << "No" << endl;
}
return 0;
}

  

POJ 2240 && ZOJ 1082 Arbitrage 最短路,c++ stl pass g++ tle 难度:0的更多相关文章

  1. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

    题目传送门 /* 最短路:Floyd模板题 只要把+改为*就ok了,热闹后判断d[i][i]是否大于1 文件输入的ONLINE_JUDGE少写了个_,WA了N遍:) */ #include <c ...

  2. POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)

    POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...

  3. poj 2240 Arbitrage 题解

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21300   Accepted: 9079 Descri ...

  4. poj 2240 Arbitrage (Floyd)

    链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...

  5. poj 2449(A*求第K短路)

    题目链接:http://poj.org/problem?id=2449 思路:我们可以定义g[x]为源点到当前点的距离,h[x]为当前点到目标节点的最短距离,显然有h[x]<=h*[x](h*[ ...

  6. POJ 1562 && ZOJ 1709 Oil Deposits(简单DFS)

    题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket . 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接 ...

  7. POJ 3076 / ZOJ 3122 Sudoku(DLX)

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

  8. poj 3100 (zoj 2818)||ZOJ 2829 ||ZOJ 1938 (poj 2249)

    水题三题: 1.给你B和N,求个整数A使得A^n最接近B 2. 输出第N个能被3或者5整除的数 3.给你整数n和k,让你求组合数c(n,k) 1.poj 3100 (zoj 2818) Root of ...

  9. POJ - 1062 昂贵的聘礼(最短路Dijkstra)

    昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u SubmitStatus Descr ...

随机推荐

  1. Android手机_软件安装目录

    1. /data/data ==> 应该是 数据存放的位置 /data/app ==> 应该是 程序存放的位置 2.

  2. 树的计数 + prufer序列与Cayley公式 学习笔记

    首先是 Martrix67 的博文:http://www.matrix67.com/blog/archives/682 然后是morejarphone同学的博文:http://blog.csdn.ne ...

  3. 线程高级应用-心得5-java5线程并发库中Lock和Condition实现线程同步通讯

    1.Lock相关知识介绍 好比我同时种了几块地的麦子,然后就等待收割.收割时,则是哪块先熟了,先收割哪块. 下面举一个面试题的例子来引出Lock缓存读写锁的案例,一个load()和get()方法返回值 ...

  4. Python学习笔记6-字典

    定义 使用键值对, >>> person = {"name":"keven","age":15,"gender& ...

  5. openfire过滤脏话插件,控制消息是否发送

    参考:http://myopenfire.com/article/getarticle/9 package com.myopenfire.plugin; import java.io.File; im ...

  6. openfire消息通知推送

    package cn.zsmy.utils.openfire; import java.io.BufferedReader; import java.io.InputStreamReader; imp ...

  7. D3.js 插入元素,删除元素

    插入元素涉及的函数有两个: 一.append():在选择集末尾插入元素 假设有三个段落元素 <p>Apple</p> <p>Pear</p> <p ...

  8. VirtualBox的网络配置,Host Only+NAT方式 (zhuan)

    http://blog.csdn.net/xinghun_4/article/details/7969894 ***************************************** 其实网 ...

  9. 20160808_Linux服务

    1. http://bbs.csdn.net/topics/370100269 2. http://blog.csdn.net/csfreebird/article/details/8239933 h ...

  10. ITERATOR(迭代器)设计模式

    1 意图:提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部表示. 2 别名(Cursor) 3 动机:队列表的访问和遍历从列表对象中分离出来放入一个迭代器对象中.   多态迭代   ...