loj 1221(spfa判正环)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25957
思路:由于路线为一个环,将路径上的权值改为c-p*d,那么然后建图,那么我们只需判断图中是否存在权值和为正的环,这个用spfa即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define MAXN 222
#define inf 1<<30 struct Edge{
int v,w;
Edge(){}
Edge(int vv,int ww):v(vv),w(ww){}
}; int n,m,flag;
int dist[MAXN],_count[MAXN];
bool mark[MAXN];
vector<vector<Edge> >g; void spfa(int st)
{
memset(mark,false,sizeof(mark));
memset(_count,,sizeof(_count));
fill(dist,dist+n+,-inf);
queue<int>que;
que.push(st);
dist[st]=;
while(!que.empty()){
int u=que.front();
que.pop();
mark[u]=false;
_count[u]++;
if(_count[u]>n){
flag=;
return ;
}
for(int i=;i<g[u].size();i++){
int v=g[u][i].v,w=g[u][i].w;
if(dist[u]+w>dist[v]){
dist[v]=dist[u]+w;
if(!mark[v]){
mark[v]=true;
que.push(v);
}
}
}
}
}
int main()
{
int _case,a,b,c,d,p,t=;
scanf("%d",&_case);
while(_case--){
scanf("%d%d%d",&n,&m,&p);
g.clear();
g.resize(n+);
while(m--){
scanf("%d%d%d%d",&a,&b,&c,&d);
g[a].push_back(Edge(b,c-d*p));
}
flag=;
for(int i=;i<n;i++)if(!flag)spfa(i);
printf("Case %d: ",t++);
flag?puts("YES"):puts("NO");
}
}
loj 1221(spfa判正环)的更多相关文章
- POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】
题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total ...
- poj1860(spfa判正环)
题目连接:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 money=(nowmoney-手续费)*rat ...
- POJ 2240 Arbitrage spfa 判正环
d[i]代表从起点出发可以获得最多的钱数,松弛是d[v]=r*d[u],求最长路,看有没有正环 然后这题输入有毒,千万别用cin 因为是大输入,组数比较多,然后找字符串用strcmp就好,千万不要用m ...
- POJ1680 Currency Exchange SPFA判正环
转载来源:優YoU http://user.qzone.qq.com/289065406/blog/1299337940 提示:关键在于反向利用Bellman-Ford算法 题目大意 有多种汇币,汇 ...
- loj 1108(spfa判负环)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26823 思路:题目的意思是求出所有的能够到达负环的点.负环很好求, ...
- [模板]SPFA判负环
目录 一.BFS法判负环 二.DFS法判负环 三.SPFA判正环 一.BFS法判负环 Code: #include<bits/stdc++.h> #define re register # ...
- poj3621 SPFA判断正环+二分答案
Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big c ...
- LightOj 1221 - Travel Company(spfa判负环)
1221 - Travel Company PDF (English) Statistics problem=1221" style="color:rgb(79,107,114)& ...
随机推荐
- ORA-00931: missing identifier ORA-06512: at "SYS.DBMS_UTILITY"
Database db = DatabaseFactory.CreateDatabase(); string sql = "SELECT * FROM table&qu ...
- TYVJ1359 收入计划
描述 高考结束后,同学们大都找到了一份临时工作,渴望挣得一些零用钱.从今天起,Matrix67将连续工作N天(1<=N<=100 000).每一天末他可以领取当天及前面若干天里没有 ...
- 还是畅通工程(MST)
还是畅通工程 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- RHEL 6.0使用CentOS yum源
引言:由于RHEL的yum在线更新是收费的,如果没有注册的话是不能使用的,即不能在线安装软件.在这种情况下,想使用RHEL系统,还想用yum源来在线安装软件,有没有办法?答案是有办法,请往下看! 1. ...
- 转SISD、MIMD、SIMD、MISD计算机的体系结构的Flynn分类法
1. 计算平台介绍 Flynn于1972年提出了计算平台的Flynn分类法,主要根据指令流和数据流来分类,共分为四种类型的计算平台,如下图所示: 单指令流单数据流机器(SISD) SISD机器是一种传 ...
- Apache VirtualHost配置
转载:http://www.cnblogs.com/wpjsolo/archive/2012/01/19/2327457.html 以lampp环境为例子,其他环境只是配置文件的路径不同. 先要在 ...
- HTML 笔记,持续更新
一.文本格式化标签 <b> 定义粗体文本. <big> 定义大号字. <em> 定义着重文字. <i> 定义斜体字. <small> 定义小 ...
- shell实现trim函数-去除字符串两侧的空格(包括tab,space键)
shell实现trim函数效果去除字符串两侧的空格,以下三个命令等价,都能实现 sed 's/^\s*//' totrim.txt |sed 's/\s*$//'>trimed.txtsed ' ...
- i686和x86_64的区别
找回TCL隐藏分区(转载) 用Wubi安装 Ubuntu 出现(Initranfs)问题的解决方案 i686和x86_64的区别 2009-04-11 08:19:31| 分类: 电脑问题 | 标 ...
- linux下用mii-tool和ethtool 查看网线是否正确连接到网卡
输入mii-tool可以查看网线是否连接到网卡#mii-tool eth0: negotiated 100baseTx-FD, link ok 有时驱动可能不支持会出错下列错误#mii-tool SI ...