POJ 3114 Tarjan+Dijkstra】的更多相关文章

题意: 间谍在战争期间想要传递一份谍报回国,谍报可以在邮局之间传递,但这种传递是单向的,并且会少耗一些时间.但是如果两个邮局在同一个国家的话,那么谍报在这两个邮局之间传递是不消耗时间的.如果几个邮局发出的谍报可以通过一些路径相互到达,那么这些邮局就属于一个国家.那么问题来了:给出一个起点和终点,问最快什么时候能够将谍报传递到. 思路: 先Tarjan缩点,然后跑Dijkstra(Floyd可能会被卡,但是貌似有个哥们[现在应该叫前辈了]多交了几次,卡1000ms过了)(也可以记忆化搜索,spfa…
题目大意: 在一个有向图中,每两点间通信需要一定的时间,但同一个强连通分量里传递信息不用时间,给两点u,v求他们最小的通信时间.   解题过程: 1.首先把强连通分量缩点,然后遍历每一条边来更新两个强联通分量之间的距离.. 2.直接Floyd会超时,应该用dijstra或者spfa做k次最短路.   犯的错误:前向星数组开的太小,一直超时.  …
POJ 3114 Countries in War 题目链接 题意:给定一个有向图.强连通分支内传送不须要花费,其它有一定花费.每次询问两点的最小花费 思路:强连通缩点后求最短路就可以 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <stack> #include <algorithm> using namesp…
题目链接 题意 : 给你两个城市让你求最短距离,如果两个城市位于同一强连通分量中那距离为0. 思路 :强连通分量缩点之后,求最短路.以前写过,总感觉记忆不深,这次自己敲完再写了一遍. #include <cstdio> #include <cstring> #include <iostream> #include <vector> #include <stack> #include <queue> #include <algor…
题目链接:http://poj.org/problem?id=3114 思路:题目要求很简单,就是求两点之间的花费的最短时间,不过有一个要求:如果这两个city属于同一个国家,则花费时间为0.如何判断一个是属于同一个国家呢?就要缩点了,这样属于同一个强连通分量的点就是属于同一个国家了.然后就是SPFA求最短路. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm>…
http://poj.org/problem?id=3114 #include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define maxn 300000 using namespace std; <<; int head[maxn],head1[maxn],dfn[maxn],low[maxn],belong[maxn],stack1[maxn],d…
做这道题的动机就是想练习一下堆的应用,顺便补一下好久没看的图论算法. Dijkstra算法概述 //从0出发的单源最短路 dis[][] = {INF} ReadMap(dis); for i = 0 -> n - 1 d[i] = dis[0][i] while u = GetNearest(1 .. n - 1, !been[]) been[u] = 1 for_each edge from u d[edge.v] = min(d[edge.v], d[u] + dis[u][edge.v]…
链接:http://poj.org/problem?id=1556 The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6216   Accepted: 2495 Description You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber wil…
Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15494   Accepted: 4100 Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors…
Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6689   Accepted: 2176 Description You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bike to school every day, you now get…
Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17383   Accepted: 4660 Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors…
先跑一边dijkstra算出从1到i的最短距离dis[i] 然后建反向边 从n开始记忆化搜索,(p,k)表示1到p的距离=dis[p]+k的方案数 答案就是$\sum\limits_{i=0}^{k}{(n,i)}$ 考虑0环,如果我记搜的时候搜到了0环,那答案就是-1,可以先用tarjan处理一下0边 看看有哪些点在零环上 (其实也可以开个栈 做到(p,k)的时候看(p,k)是不是已经在栈中了 如果是那就是-1) #include<bits/stdc++.h> #define CLR(a,x…
缩完点后对每次询问做dijkstra即可 #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<queue> #include<cmath> #include<ctime> #define pa pair<int,int> #define LL long long int using namespace…
堆优化Dij VS Spfa 堆优化Dij小胜一筹. 题目名字:Father Christmas flymouse (POJ 3160) 这题可以说是图论做的比较畅快的一题,比较综合,很想说一说. 首先题目大概意思就是走图拿点权,问说最大能拿到多少.一开始看到这题第一反应是挺好做的吧,因为每个点可以走多次,但是点权只能拿一次(可以路过不拿),这个个人觉得难度系数一下就降低了(如果每个点只能过一次就真的不会了...)于是乎,我们可以这样想,想要拿走的最大,肯定不想拿负点权,又因为每个点可以只走不拿…
题目链接: http://poj.org/searchproblem 题目大意: 飞天鼠是班长,一天班主任买了一大包糖果,要飞天鼠分发给大家,班里面有n个人,但是学生A认为学生B比自己多的糖果数目不应该大于c,如果不满足自己的条件,学生A就会向老师告状,在这个班级里面泰迪熊的编号是1,班长的编号是n,班长想和泰迪熊的糖果相差最大,问:在满足m个学生的要求后,班长与泰迪熊的糖果相差最大是多少? 解题思路: 差分约束系统,|Xa-Xb| <= c,我们假设Xa小于Xb,把糖果的最大差当成边权,因为要…
Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7298   Accepted: 2651 Description A network administrator manages a large network. The network consists of N computers and M links between pairs of computers. Any pair of computers…
蕾姐讲过的例题..玩了两天后才想起来做 貌似省赛之后确实变得好懒了...再努力两天就可以去北京玩了! 顺便借这个题记录一下求强连通分量的算法 1 只需要一次dfs 依靠stack来实现的tarjan算法 每次走到一个点 马上把它压入栈中 每次对与这个点相连的点处理完毕 判断是否low[u]==dfn[u] 若是 开始退栈 直到栈顶元素等于u才退出(当栈顶元素等于u也需要pop) 每次一起退栈的点属于同一个强连通分量 储存图可以用链式前向星也可以用邻接矩阵更可以用vector 蕾姐说不会超时 我信…
                                Countries in War Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3672   Accepted: 1057 Description In the year 2050, after different attempts of the UN to maintain peace in the world, the third world war b…
点击打开链接 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21653   Accepted: 7042 Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visi…
Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19613   Accepted: 7725 Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a li…
Time Limit: 1000MS Memory Limit: 65536K Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' s…
Skiing Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4668   Accepted: 1242   Special Judge Description Bessie and the rest of Farmer John's cows are taking a trip this winter to go skiing. One day Bessie finds herself at the top left c…
好久没写过这么长的代码了,题解东哥讲了那么多,并查集优化还是很厉害的,赶快做做前几天碰到的相似的题. #include <iostream> #include <algorithm> #include <cstdio> using namespace std; , M = 2e5 + ; ], Next[M * ]; int dfn[N], low[N], n, m, tot, num; ]; void add(int x, int y) { ver[++tot] =…
Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 37111   Accepted: 15124 Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M &…
Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19234   Accepted: 5182 Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors…
Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 37083   Accepted: 15104 Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M &…
SPF Description Consider the two networks shown below. Assuming that data moves around these networks only between directly connected nodes on a peer-to-peer basis, a failure of a single node, 3, in the network on the left would prevent some of the s…
题意:如果v点能到的所有点反过来又能到v点,则v点是sink点,排序后输出所有的sink点. 思路:Tarjan缩点,输出所有出度为0的连通块内的点. PS:一定要记得把数组清零!!!!!!!否则自己怎么死的都不知道. 原题请戳这里 #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cstring> #include<algorithm>…
题意:有n(n<=10000)头牛,每头牛都想成为最受欢迎的牛,给出m(m<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但是不是相互的,那么就是说1欢迎2不代表2欢迎1,但是如果2欢迎3那么1也欢迎3. 输入第一行为n,m第2到1+m行为m个欢迎关系,求被所有牛都欢迎的牛的数量. 思路:Tarjan求强联通分量做. 1.如果图不联通,直接输出零.(不解释) 2.如果有超过1个出度=0的点,直接输出零.因为它肯定不是最受欢迎的牛. 3.如果只有一个出度等于零的点,那它的强联通分…
这道题认真想了想.. [ 题目大意:有N个学校,从每个学校都能从一个单向网络到另外一个学校,两个问题 1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件. 2:至少需要添加几条边,使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件. 解题思路: 首先找连通分量,然后看连通分量的入度为0点的总数,出度为0点的总数,那么问要向多少学校发放软件,就是入度为零的个数,这样才能保证所有点能够找到 然后第二问添加多少条边可以得到使整个图达到一个强连通分量,答…