hdu 3357 Stock Chase【floyd判成环】

Stock Chase

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1589    Accepted Submission(s): 485


Problem Description
I have to admit, the solution I proposed last year for solving the bank cash crisis didn’t solve the whole economic crisis. As it turns out, companies don’t have that much cash in the first place.
They have assets which are primarily shares in other companies. It is common, and acceptable, for one company to own shares in another. What complicates the issue is for two companies to own shares in each other at the same time. If you think of it for a moment, this means that each company now (indirectly) controls its own shares.
New market regulation is being implemented: No company can control shares in itself, whether directly or indirectly. The Stock Market Authority is looking for a computerized solution that will help it detect any buying activity that will result in a company controlling its own shares. It is obvious why they need a program to do so, just imagine the situation where company A buying shares in B, B buying in C, and then C buying in A. While the first two purchases are acceptable.
The third purchase should be rejected since it will lead to the three companies controlling shares in themselves. The program will be given all purchasing transactions in chronological order. The program should reject any transaction that could lead to one company controlling its own shares.
All other transactions are accepted.
 

Input
Your program will be tested on one or more test cases. Each test case is specified on T + 1 lines. The first line specifies two positive numbers: (0 < N <= 234) is the number of companies and (0 < T <= 100, 000) is the number of transactions. T lines follow, each describing a buying transaction. Each transaction is specified using two numbers A and B where (0 < A,B <= N) indicating that company A wants to buy shares in company B.
The last line of the input file has two zeros.
 

Output
For each test case, print the following line:
k. R
Where k is the test case number (starting at one,) R is the number of transactions that should be rejected.
Note: There is a blank space before R.
Sample Input
3 6
1 2
1 3
3 1
2 1
1 2
2 3
0 0
 


Sample Output
1. 2



 

题目大意:给出n个点,m条路径,边都是有向边,从上至下建路,如果有重边忽略掉,如果建的路会成环,那么这条路就不需要建,问最终不需要建的路的总数。

思路:临街矩阵图如果map【i】【j】==1&&map【j】【i】==1,那么这就说明从i能到j,从j也能到i,那么就成环咯~建边的时候,我们只能建出当前边,例如:

1 2

2 3

map【1】【2】=1,map【2】【3】=1;我们也知道这里map【1】【3】也应该是1,但是我们入图的只有两个边,并没有这条边,所以我们采用floyd的递推思想,如果

map【i】【j】==1&&map【j】【k】==1那么map【i】【k】=1;floyd算法计算最短路的时候是枚举i,k点对,通过点j来压缩路径。但是只通过一个k来压缩路径是不行的,所以floyd算法核心一共三层for,但是这里题目数据很大很大,每一次入图一条边都floyd三层for遍历是一定会超时的,所以我们枚举k的时候只枚举入图的那条边的起点和终点即可。

没有优化的情况下是951ms过的,我这里加上FAST IO,800+ms,简单优化代码核心部分,最终AC 530ms。

AC代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int map[250][250];
int n,m;
int ans;
int read()
{
    int res = 0, ch, flag = 0;

    if((ch = getchar()) == '-')             //判断正负
        flag = 1;

    else if(ch >= '0' && ch <= '9')           //得到完整的数
        res = ch - '0';
    while((ch = getchar()) >= '0' && ch <= '9' )
        res = res * 10 + ch - '0';

    return flag ? -res : res;
}
void solve(int x,int y)
{
    if(map[x][y]==1)
    {
        return ;
    }
    if(map[y][x]==1||x==y)
    {
        ans++;
        return ;
    }
    map[x][y]=1;
    for(int i=1;i<=n;i++)
    {
        if(map[i][x]==0)continue;
        for(int j=1;j<=n;j++)
        {
            if(map[i][x]==1&&map[x][j]==1)
            {
                map[i][j]=1;
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(map[i][y]==0)continue;
        for(int j=1;j<=n;j++)
        {
            if(map[i][y]==1&&map[y][j]==1)
            {
                map[i][j]=1;
            }
        }
    }
}
int main()
{
    int kase=0;
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)break;
        ans=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                map[i][j]=0;
            }
        }
        while(m--)
        {
           int x,y;
           x=read();
           y=read();
           solve(x,y);
        }
        printf("%d. %d\n",++kase,ans);
    }
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值