CF792A New Bus Route 题解】的更多相关文章

Content 给定一个长度为 \(n\) 的数列 \(a_1,a_2,a_3,...,a_n\),求这个序列当中差的绝对值最小的数对并求出这样的数对的个数. 数据范围:\(2\leqslant n\leqslant 2\times 10^5,-10^9\leqslant a_i\leqslant 10^9\). Solution 先把这个数对排序,然后一个一个去比较得到差的绝对值的最小值,最后再去一个一个比较看差的绝对值的最小值是否等于这个数对的差的绝对值即可. Code #include <…
UVA - 1349 Optimal Bus Route Design Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Description A big city wants to improve its bus transportation system. One of the improvement is to add scenic routes which go es through attrac…
/** 题目:UVA1349 Optimal Bus Route Design 链接:https://vjudge.net/problem/UVA-1349 题意:lrj入门经典P375 给n个点(n<=100)的有向带权图,找若干个有向圈,每个点恰好属于一个圈.要求权和尽量小.注意即使(u,v) 和(v,y)都存在,他们的权值也不一定相同. 思路:拆点法+最小费用最佳完美匹配. 如果每个点都有一个唯一的后继(不同的点没有相同的后继点,且只有一个后继),那么每个点一定恰好属于一个圈. 联想到二分…
4. D - Optimal Bus Route Design 题意:给出n(n<=100)个点的带权有向图,找出若干个有向圈,每个点恰好属于一个有向圈.要求权和尽量小. 注意即使(u,v)和(v,u)都存在,他们的权值也不一定相同. 思路:每个点恰好属于一个有向圈,意味着每个点都有一个唯一的后继.某个东西恰好有唯一的-..这便是二分图匹配的特点 .将每个结点拆成Xi和Yi,则原图中的有向边u->v对应二分图中的边Xu->Yv.当流量满载时存在,存在完美匹配,否则不存在 . 解决二分图完…
题意:给定一个有向带权图,找若干个环,使得每个点属于且仅属于一个环,要求使得环权值之和最小 题解:发现这题中每个点属于且仅属于一个环,这时候"仅"这种恰好的含义,让我们想到了匹配问题 当每一个点有且只有一个后继之时,会满足题目的要求,于是把点i拆成i和i',每条边由x连向y',这样做一下二分图最优完美匹配即可 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstr…
题意:给定一个 n 个点的有向带权图,让你找若干个圈,使得每个结点恰好属于一个圈,并且总长度尽量小. 析:一开始想的是先缩点,先用DP,来求... 题解给的是最小费用流或者是最佳完全匹配,其实都是一样的,因为每个点都只属于一个圈,那么对于每个点的入度和出度都应该是一样的,然后就是把每个点都拆成两个点,然后如果有边相连,就加一条费用该权值,容量为1的边,然后跑一个最小费用流即可,如果满流就是有解,否则就是无解.如果用最佳完全匹配的话,也差不多,每条都有一个后继边,连一条边,然后不存在的用无限大,因…
转载请注明:http://blog.csdn.net/jiangshibiao/article/details/23989499 [原题] 1266: [AHOI2006]上学路线route Time Limit: 3 Sec  Memory Limit: 162 MB Submit: 1084  Solved: 360 [Submit][Status] Description 可可和卡卡家住合肥市的东郊.每天上学他们都要转车多次才干到达市区西端的学校. 直到有一天他们两人參加了学校的信息学奥林…
二分图最小权完美匹配. 一个最小费用流就能跑了,记住检查一下,容量是否跑满,如果没有跑满,就说明没有完美匹配. #include<cstdio> #include<algorithm> #include<cstring> using namespace std; +; + ; const int inf = 0x3f3f3f3f; int g[maxn],v[maxm],f[maxm],c[maxm],nex[maxm],eid; ],vid; int n,ans,S,…
题意: 给出一个有向带权图,找到若干个圈,使得每个点恰好属于一个圈.而且这些圈所有边的权值之和最小. 分析: 每个点恰好属于一个有向圈 就等价于 每个点都有唯一后继. 所以把每个点i拆成两个点,Xi 和 Yi ,然后求二分图最小权完美匹配(流量为n也就是满载时,就是完美匹配). #include <bits/stdc++.h> using namespace std; + ; ; struct Edge { int from, to, cap, flow, cost; Edge(int u,…
题意: 给若干景点,每个景点有若干单向边到达其他景点,要求规划一下公交路线,使得每个景点有车可达,并且每个景点只能有1车经过1次,公车必须走环形回到出发点(出发点走2次).问是否存在这样的线路?若存在就给出所有公交车需要走过的路的长度,要求长度尽量小. 分析: 这超级难发现是网络流来做的.要将每个点归结到某个环上,那么环上的点都是只有1个前驱,1个后继.如果1个前驱配1个后继,就是匹配问题了.但是这样的匹配有点混杂,所以要拆点,将1个点拆成2个,分别处于X和Y集中,然后根据有向边建图.成了带权二…
题意: 给出n个点,以及每个点到其他点的有向距离,要求设计线路使得每一个点都在一个环中,如果设计的线路拥有最小值,那么这个线路就是可选的.输出这个最小值或者说明最小线路不存在. 思路: 在DAG的最小路径覆盖中,找到的最大匹配数实际是非终点的点的最大数量(每一个匹配对应一个起点),点数减去这个数量就是终点的最少数量,一个终点对应一条路径,所以这个路径覆盖是最小的. 这个题要求每一个点都在一个环中,也就是说找到一个设计线路的方案使之不存在DAG,那么自然就没有终点存在,也就意味着每一个点都可以作为…
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4095 题意: 给n个点(n≤99)的有向带权图,找若干个有向圈,使得每个点恰好属于一个圈.要求权和尽量小.注意即使(u,v)和(v,u)都存在,它们的权值也不一定相同. 分析: 每个点恰好属于一个有向圈,意味着每个点都有一个唯一的后继.反过来,只要每个点都有唯一的后继,每个点一定恰…
题意:给定一个有向图,让你找出若干个图,使得每个点恰好属于一个圈,并且总的权和最小. 析:每个点都有唯一的一个圈,也就是说每一点都有唯一的后继,那么我们就可以转换成求一个图的最小权的最佳完全匹配,可以用最小费用流来求, 先把每个结点拆成两个点,假设是x,y,然后建立一个源点,向每个点的x连一条容量为1的边,建立一个汇点,每个点的y向汇点连一条容量为1的边, 每条边u,v,也连接一条容量为1,费用为权值的边,最小求一个最小费用流即可. 代码如下: #pragma comment(linker, "…
恰好属于一个圈,那等价与每个点有唯一的前驱和后继,这让人想到了二分图, 把一个点拆开,点的前驱作为S集和点的后继作为T集,然后连边,跑二分图最小权完美匹配. 写的费用流..最大权完美匹配KM算法没看懂 #include<bits/stdc++.h> using namespace std; +; struct Edge { int v,cap,cost,nxt; }; vector<Edge> edges; #define PB push_back int head[maxn];…
题意: 给定n个点的有向图问,问能不能找到若干个环,让所有点都在环中,且让权值最小,KM算法求最佳完美匹配,只不过是最小值,所以把边权变成负值,输出时将ans取负即可 这道题是在VJ上交的 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; + ; const int inf = 0x3f3f3f3f; bool…
题意:有一个N个点的有向带权图,要求找若干个有向圈,使得每个点恰好属于一个圈.请输出满足以上条件的最小权和. 解法:有向圈?也就是每个点有唯一的后继.这是一个可逆命题,同样地,只要每个点都有唯一的后继,那么它们一定恰好属于一个圈.而"唯一"可以想到二分图匹配.把每个点拆成两个点,分别放在二分图的两边.两侧的点连的边就是原来的边的转化,另外再给源点和汇点分别连 n 条容量为1.费用为0的边.这样就保证了每个点有唯一的后继.再由于是要求所有点都属于一个圈,也就是完美匹配,就判断一下是否满流…
Description A bus moves along the coordinate line Ox from the point x = 0 to the point x = a. After starting from the point x = 0, it reaches the point x = a, immediately turns back and then moves to the point x = 0. After returning to the point x = …
A. Fair Game time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Petya and Vasya decided to play a game. They have n cards (n is an even number). A single integer is written on each card. Befor…
C. Bus time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A bus moves along the coordinate line Ox from the point x = 0 to the point x = a. After starting from the point x = 0, it reaches the…
C. Bus time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A bus moves along the coordinate line Ox from the point x = 0 to the point x = a. After starting from the point x = 0, it reaches the…
We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For example if routes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->... forever…
We have a list of bus routes. Each routes[i]is a bus route that the i-th bus repeats forever. For example if routes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->... forever.…
C. Bus time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input output: standard output A bus moves along the coordinate line Ox from the point x = 0 to the point x = a. After starting from the point x = 0, it reaches…
We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For example if routes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->... forever…
A. Serval and Bus time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output It is raining heavily. But this is the first day for Serval, who just became 3 years old, to go to the kindergarten. Unfort…
We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For example if routes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->... forever…
题目如下: We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For example if routes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->... f…
inputstandard inputoutputstandard outputIt is raining heavily. But this is the first day for Serval, who just became 3 years old, to go to the kindergarten. Unfortunately, he lives far from kindergarten, and his father is too busy to drive him there.…
  计蒜客)翻硬币 //暴力匹配 #include<cstdio> #include<cstring> #define CLR(a, b) memset((a), (b), sizeof((a))) using namespace std; int n, m, t; int main() { int i, j, x; scanf("%d", &t); while(t--) { scanf("%d%d", &n, &m)…
1349 - Optimal Bus Route Design Time limit: 3.000 seconds A big city wants to improve its bus transportation system. One of the improvement is to add scenic routes which go es through attractive places. Your task is to construct a bus-route-plan for…