nyoj973 天下第一(spfa判环)

天下第一

时间限制:1000 ms  |  内存限制:65535 KB

难度:3

描述

AC_Grazy一直对江湖羡慕不已,向往着大碗吃肉大碗喝酒的豪情,但是“人在江湖漂,怎能

不挨刀",”人在江湖身不由己",如果自己的武功太差,在江湖会死的很惨,但是AC_Grazy没有

武功秘籍练不了绝世武功.有道是“山重水复疑无路,柳暗花明又一村”,在AC_Grazy家里面

竟然藏着一本书,书名竟然叫做【超级外挂】,竟然能在各种武功之间进行转化,据说是他爷

爷的爷爷的...爷爷传下来的...

闲着无事便拿来看看,只看一眼便再也停不下了,只见上面写着“纵横武林打遍天下无敌手武功心法秘籍收录”.

翻开第一篇一看竟然是【降龙十八掌】...

心法只是一个修练武功的途径,重要的是真气的多少,于是他便想利用外挂让武功之间进行转

化,来让真气无限增加,但是这个心法只能按照顺序转化,我们分别用 1号和2号来代替两种功法 当然转化会有一定的转化率f

比如1 0.5 2 便是把 1的一半真气转化给2 ,为了简化问题,我们每次都从1号秘籍开始进行转化,如果其中一个秘籍转化断了,那么以后的功法就不能转换。

输入
输入:首先输入一个数 T(T<=20)表示T组数据

然后输入两个数n(2<=n<=500)和m(1=<m<=2000)分别表

示有n种秘籍,随后的m行分别输入

秘籍u(n>=u>0) 转化率 f (0<f<=10)秘籍 v.(0<v<=n)

输出
输出:如果可以无限增加真气输出Yes否则输出No.
样例输入
2
3 3
1 2 2
2 2 3
3 2 1
4 3
1 2 2
3 2 4
4 2 3
样例输出
Yes
No
上传者
ACM_王亚龙

以前做过一次 不过那时候不够深刻了解判断成环的原理

今天仔细又看了一遍 当某个顶点进入队列的次数大于它的入度时候 可以判断已经成环

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
double f[505][505],dist[505];//f为转换率 dist源点到各点的路径
int n,m,sum[505];//n为点的个数,m为边的个数,sum为进队列的次数
int in_degree[505];//入度  判断是否成环
bool in[505];//判断是否在队列中
vector<int>list[505];//邻接表
queue<int>s;
bool spfa(int pos)
{
    while(!s.empty())
    s.pop();
    sum[pos]++;
    in[pos]=true;
    dist[pos]=1.0;
    s.push(pos);
    while(!s.empty())
    {
        pos=s.front();
        s.pop();
        in[pos]=false;
        for(int i=0;i<list[pos].size();i++)
        {
            int x=list[pos][i];
            if(dist[x]<dist[pos]*f[pos][x])
            {
                dist[x]=dist[pos]*f[pos][x];
                if(!in[x])
                {
                    s.push(x);
                    in[x]=true;
                    //已经成环 真气可以无限增加
                    if(++sum[x]>in_degree[x])
                    return true;
                }
            }
        }
    }
    return false;
}
int main()
{
    int ncase;
    scanf("%d",&ncase);
    while(ncase--)
    {
        memset(f,0,sizeof(f));
        memset(list,0,sizeof(list));
        memset(in,false,sizeof(in));
        memset(sum,0,sizeof(sum));
        memset(dist,0,sizeof(dist));
		memset(in_degree,0,sizeof(in_degree));
        scanf("%d %d",&n,&m);
        for(int i=0;i<m;i++)
        {
            int a,b;
            double x;
            scanf("%d %lf %d",&a,&x,&b);
            f[a][b]=max(f[a][b],x);
			in_degree[b]++;
			//建邻接表
            list[a].push_back(b);
        }
        if(spfa(1))
            printf("Yes\n");
        else
            printf("No\n");  

    }
}  
时间: 2024-08-25 21:34:47

nyoj973 天下第一(spfa判环)的相关文章

POJ 2240 Arbitrage (spfa判环)

Arbitrage Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 Frenc

poj2240 Arbitrage (spfa判环)

Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10997   Accepted: 4622 Description Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currenc

hdu 1317 XYZZY(spfa判环)

http://acm.hdu.edu.cn/showproblem.php?pid=1317 大致题意:有n个房间,每个房间都有对应的能量值(可正可负),现在从1出发要到达n,初始能量为100,问是否能够达到n点,到达n的条件是中间及最后的能量值都要大于0. 思路:若不考虑环,那么求最长路判断是否大于0即可.若存在负环,对求最长路也没影响:但当存在正环时,最长路就不存在了.可用spfa判断,当某点入队超过n次,那么它必定在环中,直接将其dis置为INF,并不再将其近队列.最后若能到达n则可行,否

【BZOJ 3232】圈地游戏 二分+SPFA判环/最小割经典模型

最小割经典模型指的是“一堆元素进行选取,对于某个元素的取舍有代价或价值,对于某些对元素,选取后会有额外代价或价值”的经典最小割模型,建立倒三角进行最小割.这个二分是显然的,一开始我也是想到了最小割的那个模型的但是我觉得他会不是一个圈我就否掉了,但是仔细想想的话会发现,如果是这样的话所得到的答案一定小于等于一个圈的答案(浓度),所以我们可定会得到最终答案,所以这样做是可以的,所以说要有宽松得正解的意识(泥沙俱下但沙子不影响我泥).当时我否掉最小割以后就立马去想费用流了,然后想到建图后发现那样建图虽

poj 3259 Wormholes(spfa 判环)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; #define INF 100000000 int u[6000],v[6000],w[6000]; int first[6000],next[6000]; int coun[6000]; __int64 d[6000]; in

poj1860(spfa判正环)

题目连接:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 money=(nowmoney-手续费)*rate,现在问你有v钱,从s开始出发交换钱能不能赚钱. 分析:如何存在正环,能无限增加钱,肯定可以赚了,因此用spfa判一下即可 #include <cstdio> #include <cstring> #include <string> #include <cmath&

Poj3259--Wormholes(Spfa 判负环)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36836   Accepted: 13495 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way p

POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1860 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two par

LightOj 1221 - Travel Company(spfa判负环)

1221 - Travel Company PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB A travel company is planning to launch their bus service in a new route. So they conducted a survey and made a list of all possible roads connecting diff