CF1100E Andrew and Taxi

首先我们发现,本题具有可二分性。若花费x可以完成,x+1也一定可以完成。

那么判断是否可行,可以把二分得到的mid作为下限,仅连接边权大于等于mid的边,如果这样的图有环,那么向上二分,否则向下。

这样的正确性显然,因为如果图是一个DAG,那么剩下的边始终从拓扑序小的向大的连,这样就不会出现环。

输出方案的思路也是如此。

#include<iostream>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<ctime>
#include<queue>
#include<cstdlib>
#include<algorithm>
#define N 1100000
#define L 1000000
#define eps 1e-7
#define inf 1e9+7
#define db double
#define ll long long
#define ldb long double
using namespace std;
inline int read()
{
    char ch=0;
    int x=0,flag=1;
    while(!isdigit(ch)){ch=getchar();if(ch==‘-‘)flag=-1;}
    while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-‘0‘;ch=getchar();}
    return x*flag;
}
queue<int>q;
int n,m,num,rk[N],ans[N],head[N],degree[N];
struct edge{int to,nxt;}e[N*2];
inline void add(int x,int y){e[++num]=(edge){y,head[x]};head[x]=num;}
struct link{int x,y,z,id;}p[N];
bool cmp(link a,link b){return a.z<b.z;}
bool toposort()
{
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        rk[i]=0;
        if(!degree[i])q.push(i);
    }
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        rk[x]=++cnt;
        for(int i=head[x];i!=-1;i=e[i].nxt)
        {
            int to=e[i].to;
            if(!(--degree[to]))q.push(to);
        }
    }
    return cnt==n;
}
int main()
{
    n=read();m=read();
    for(int i=1;i<=m;i++)p[i].x=read(),p[i].y=read(),p[i].z=read(),p[i].id=i;
    sort(p+1,p+m+1,cmp);
    int l=0,r=m,mid;
    while(l<r)
    {
        mid=(l+r)>>1;
        num=-1;for(int i=1;i<=n;i++)head[i]=-1,degree[i]=0;
        for(int i=mid+1;i<=m;i++)add(p[i].x,p[i].y),degree[p[i].y]++;
        if(toposort())r=mid;else l=mid+1;
    }
    num=-1;for(int i=1;i<=n;i++)head[i]=-1,degree[i]=0;
    for(int i=l+1;i<=m;i++)add(p[i].x,p[i].y),degree[p[i].y]++;
    toposort();
    int cnt=0,maxn=0;
    for(int i=1;i<=l;i++)if(rk[p[i].x]>rk[p[i].y])maxn=max(maxn,p[i].z),ans[++cnt]=p[i].id;
    sort(ans+1,ans+cnt+1);
    printf("%d %d\n",maxn,cnt);
    for(int i=1;i<=cnt;i++)printf("%d ",ans[i]);
    return 0;
}

原文地址:https://www.cnblogs.com/Creed-qwq/p/10293809.html

时间: 2024-07-31 07:33:22

CF1100E Andrew and Taxi的相关文章

CF-1100E Andrew and Taxi

CF-1100E Andrew and Taxi https://codeforces.com/contest/1100/problem/E 知识点: 二分 判断图中是否有环 题意: 一个有向图,每边有一条边权,对于一个值x,可以使得边权小于等于x的边反转,求最小的x,使得某些边反转之后图中没有环 分析:Suppose we have k traffic controllers. They can turn all edges whose weight is less than or equal

Andrew and Taxi CodeForces - 1100E (思维,拓扑)

大意: 给定有向图, 每条边有一个权值, 假设你有$x$个控制器, 那么可以将所有权值不超过$x$的边翻转, 求最少的控制器数, 使得翻转后图无环 先二分转为判定问题. 每次check删除能动的边, 若剩余图有环显然不成立, 否则将剩余的图拓排一下, 再把能动的边按拓排的方向即可保证无环. #include <iostream> #include <algorithm> #include <math.h> #include <cstdio> #include

[CSP校内集训]pestc(拓扑排序)

题意 给一个边带权的有向图,可以花费边权使得一条边反向:通过翻转边让原图变成一个DAG,要求是所有花费中的最大值最小\(,(n,m\leq 200000)\),保证无重边和自环 解法1 考场上没看出来性质,于是口胡了一个乱搞做法 连好边后直接对原图进行一遍拓扑排序,由于原图不是DAG,所以会有无法入队的环存在:如果当前队列为空而有点没有被遍历到,那么就强行选择一个点将连向它的边翻转: 具体的,我们选择\((max(\) 连向\(i\)的边 \())\)最小的\(i\),由于翻转了连向\(i\)的

E - Andrew and Taxi-二分答案-topo判环

E - Andrew and Taxi 思路 :min max   明显二分答案,二分需要破坏的那些边的中机器人数量最多的那个. check 过程建边时直接忽略掉小于 mid 的边,这样去检验有无环存在即可. 当时有一点担心会出现 有一个环 有一条边 反过来之后 这个环破坏了 却成就了 另一个环,但是画图发现 这样的图 ,它们两边会形成 一个大环.那个大环一定会通过别的边破坏掉,所以不需要担心这种情况.topo 判环即可. #include<bits/stdc++.h> using names

Codeforces Round #532 (Div. 2)

D.Dasha and chess 题意 有666个黑棋子在一个999*999的棋盘上,你只有一个白棋子,黑棋子可以走到棋盘的任何地方,白棋可以走到九宫格内的点,你和交互器轮流下棋(每次只能操作一个棋子),白棋与任何一个黑棋在同一行/同一列就算你赢,给定一个局面,让白棋赢 由鸽巢定理 以(500,500)为中心,四个象限内一定存在一个象限的棋子 \(\leq 666 / 4\),剩下三个象限棋子的和一定 \(\geq 666 / 4 * 3 = 500\),走到(500,500)后沿着到棋子最少

[C1] Andrew Ng - AI For Everyone

About this Course AI is not only for engineers. If you want your organization to become better at using AI, this is the course to tell everyone--especially your non-technical colleagues--to take. In this course, you will learn: The meaning behind com

斯坦福大学机器学习(Andrew [email&#160;protected])--自学笔记

今天学习Andrew NG老师<机器学习>之6 - 6 - Advanced Optimization,做笔记如下: 用fminunc函数求代价函数最小值,分两步: 1.自定义代价函数 function [jVal,gradient] = costFunction(theta)jVal = (theta(1)-5)^2 + (theta(2)-5)^2;gradient = zeros(2,1);gradient(1) = 2*(theta(1)-5);gradient(2) = 2*(the

URAL 2005. Taxi for Programmers (最短路 数学啊)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2005 2005. Taxi for Programmers Time limit: 0.5 second Memory limit: 64 MB The clock shows 11:30 PM. The sports programmers of the institute of maths and computer science have just finished their trai

Timus 2005. Taxi for Programmers 题解

The clock shows 11:30 PM. The sports programmers of the institute of maths and computer science have just finished their training. The exhausted students gloomily leave their computers. But there's something that cheers them up: Misha, the kind coach