【Gym - 101986F】Pizza Delivery(Dijkstra最短路,建图方式,反向建图,Tarjan求桥,图论模板)

题干:

题目大意:

一个有向图,编号1~n的n个点,m条边,规定1为起点,2为终点,问对于每一条边,反转它的方向,最短路会不会发生改变,如果变短了,输出HAPPY,变长了或者到达不了了输出SAD,不变的话输出SOSO。

解题报告:

建三个图,正向图G1,反向图G2,对于G1的边(u,v),假如G1.d[u]+G2.[v]+w==G1.d[2],那么他就是最短路上的一条路径,用这些路径重新建一个图G3,那么显然这个G3中任意一条点1到点2的路径都是最短路,对G3跑一遍tarjan求个桥,标记处理一下对应G1的哪条边,记下编号。然后枚举G1的每一条边,如果G1.d[v]+G2.[u]+w==G1.d[2],那么就是HAPPY,否则分两种情况:如果这条边是桥,那么改变它的方向是会改变新图的连通性的,必定不能到达2点,输出SAD;如果不是桥,那么就是SOSO,因为有别的方式可以到达点2。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
struct Edge {int fr,to;ll w;int ne;
//	Edge(int fr=0,int to=0,ll w=0,int ne=-1):fr(fr)
};
struct Graph {struct Point {int pos;ll c;Point(){}Point(int pos,ll c):pos(pos),c(c){}bool operator<(const Point b) const {return c > b.c;}};Edge e[MAX];int tot = 0;//总共tot条边,编号0~(tot-1) int head[MAX],id[MAX];bool vis[MAX];ll d[MAX];void add(int u,int v,ll w) {e[tot].fr = u;e[tot].to = v;e[tot].ne = head[u];e[tot].w = w;head[u] = tot++; }void Dijkstra(int st) {priority_queue<Point> pq;memset(d,0x3f,sizeof d);memset(vis,0,sizeof vis);d[st] = 0;pq.push(Point(st,0));while(pq.size()) {Point cur = pq.top();pq.pop();if(vis[cur.pos]) continue;vis[cur.pos] = 1; for(int i = head[cur.pos]; ~i; i = e[i].ne) {if(d[e[i].to] > d[cur.pos] + e[i].w) {d[e[i].to] = d[cur.pos] + e[i].w;pq.push(Point(e[i].to,d[e[i].to]));}}}}///int clk = 0;int DFN[MAX],LOW[MAX],bi[MAX];void id_add(int u,int v,ll w,int _id) {e[tot].fr = u;e[tot].to = v;e[tot].ne = head[u];e[tot].w = w;id[tot] = _id;//记录编号为tot的这条边是第几个点的 head[u] = tot++; }void tarjan(int x,int rt) {DFN[x] = LOW[x] = ++clk;for(int i = head[x]; ~i; i = e[i].ne) {if(e[i].to == rt) continue; if(!DFN[e[i].to]) {tarjan(e[i].to,x);LOW[x] = min(LOW[x],LOW[e[i].to]);if(LOW[e[i].to] > DFN[x]) {
//					printf("%d**%d\n",e[i].fr,e[i].to);bi[id[i]] = 1;}}else LOW[x] = min(LOW[x],DFN[e[i].to]);
//或者不用判重边了直接用这一句
//			else if(e[i].to != rt)LOW[x] = min(LOW[x],DFN[e[i].to]);
//而且其实不应该是直接用这一句 而应该是传参的时候传入边的编号rt,这样我们else if(i != (rt^1))这样才对,因为这代表的才是,不是祖先边的回边。所以这里严格来说不能直接判断是否是祖先节点,因为还有可能有重边或者自环的情况,但是这题因为数据保证了所以无所谓。 }}void init() {tot = 0;clk = 0;memset(head,-1,sizeof head);}
} G1,G2,G3; 
int main()
{int n,m;int u,v;ll w;cin>>n>>m;G1.init();G2.init();G3.init();//G1是原图,G2是反图,G3是最短路无向子图 for(int i = 1; i<=m; i++) {scanf("%d%d%lld",&u,&v,&w);G1.add(u,v,w);G2.add(v,u,w);//加反边 }G1.Dijkstra(1);G2.Dijkstra(2);ll DIS = G1.d[2];
//构造G3这个最短路子图。 for(int i = 0; i<G1.tot; i++) {u = G1.e[i].fr;v = G1.e[i].to;w = G1.e[i].w;if(G1.d[u] + G2.d[v] + w == DIS) {
//			printf("%d@@@@@@%d\n",u,v);G3.id_add(u,v,w,i);G3.id_add(v,u,w,i);}}G3.tarjan(1,-1);//改成0也可以过。for(int i = 0; i<G1.tot; i++) {u = G1.e[i].fr;v = G1.e[i].to;w = G1.e[i].w;if(G1.d[v] + G2.d[u] + w < DIS) puts("HAPPY");else if(G3.bi[i]) puts("SAD");else puts("SOSO");} return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440537.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

学java好还是web前端好_到底是学习Java好,还是Web前端好?

要想之后的发展空间比较大的话&#xff0c;我个人的建议还是要往全栈Web前端开发优势&#xff1a;人才缺口大&#xff0c;发展前景好&#xff0c;需求量大前景指数&#xff1a;★★★★★web前端开发前景大好&#xff01;其中的HTML5技术更是日趋成熟&#xff0c;HTML5是移动互…

【CF#505B】Mr. Kitayuta's Colorful Graph (并查集或Floyd或BFS)

题干&#xff1a; Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to p…

java data jpa_Spring Data JPA(一)简介

Spring Data JPA介绍可以理解为JPA规范的再次封装抽象&#xff0c;底层还是使用了Hibernate的JPA技术实现&#xff0c;引用JPQL(Java Persistence Query Language)查询语言&#xff0c;属于Spring整个生态体系的一部分。随着Spring Boot和Spring Cloud在市场上的流行&#xff0…

【CodeForces - 438D】The Child and Sequence(线段树区间取模操作)

题干&#xff1a; At the childrens day, the child came to Pickss house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks. Fortunately, Picks remembers how to repair the sequ…

java 自定义xml_6.1 如何在spring中自定义xml标签

dubbo自定义了很多xml标签&#xff0c;例如&#xff0c;那么这些自定义标签是怎么与spring结合起来的呢&#xff1f;我们先看一个简单的例子。一 编写模型类1 packagecom.hulk.testdubbo.model;23 public classHero {4 privateString name;5 private intage;67 publicString ge…

【POJ - 3723】Conscription (最大生成树,最小生成树MST变形)

题干&#xff1a; Windy has a country, and he wants to build an army to protect his country. He has picked up N girls and M boys and wants to collect them to be his soldiers. To collect a soldier without any privilege, he must pay 10000 RMB. There are some…

java 监听本地端口_Java-在本地端口上侦听RTP数据包

因此,我目前正在开发一个客户端应用程序,该应用程序在端口5004上侦听RTP数据包.由于可能有多个服务器发送RTP数据包,因此我无法使用套接字连接到特定的远程主机.相反,我尝试了以下方法来侦听本地端口&#xff1a;Socket socket new Socket("127.0.0.1", 5004);Socke…

【FZU - 2254】英语考试(最小生成树,思维,建图)

题干&#xff1a; 在过三个礼拜&#xff0c;YellowStar有一场专业英语考试&#xff0c;因此它必须着手开始复习。 这天&#xff0c;YellowStar准备了n个需要背的单词&#xff0c;每个单词的长度均为m。 YellowSatr准备采用联想记忆法来背诵这n个单词&#xff1a; 1、如果Ye…

java 模块设计模式_Java9模块化学习笔记二之模块设计模式

模块设计的原则:1、防止出现编译时循环依赖(主要是编译器不支持)&#xff0c;但运行时是允许循环依赖的&#xff0c;比如GUI应用2、明确模块的边界几种模块设计:API模块&#xff0c;聚合模块(比如java.base)可选依赖两种方式:1、可选的编译时依赖(类似于maven的provided scope)…

【HDU - 5627】Clarke and MST(最大生成树,与运算性质,最小生成树MST变形)

题干&#xff1a; Clarke is a patient with multiple personality disorder. One day he turned into a learner of graph theory. He learned some algorithms of minimum spanning tree. Then he had a good idea, he wanted to find the maximum spanning tree with bit …

java爬虫获取div内容_Java爬虫-简单解析网页内容

获取百度新闻中所有的中国新闻的标题时间来源1 获取网页2 public static String getContent(String str) throwsClientProtocolException, IOException {3 CloseableHttpClient closeableHttpClientHttpClients.createDefault(); //创建实例4 HttpGet httpGetnewHttpGet(str);5…

【HDU - 4635】Strongly connected(缩点,新图性质,建图,Tarjan求强连通分量)

题干&#xff1a; Give a simple directed graph with N nodes and M edges. Please tell me the maximum number of the edges you can add that the graph is still a simple directed graph. Also, after you add these edges, this graph must NOT be strongly connected.…

python bind sock_python 在bind端口之后创建的socket如果不关闭的话会被回收吗?

在进行tcpsocket编程的时候&#xff0c;遇到一个问题&#xff1a;我创建一个Asocket&#xff0c;在进行bind之后&#xff0c;这个socket为 在进行tcpsocket编程的时候&#xff0c;遇到一个问题&#xff1a; 我创建一个Asocket&#xff0c;在进行bind之后&#xff0c;这个socket…

【ZOJ - 2955】Interesting Dart Game(背包,结论,裴蜀定理,数论)

题干&#xff1a; Recently, Dearboy buys a dart for his dormitory, but neither Dearboy nor his roommate knows how to play it. So they decide to make a new rule in the dormitory, which goes as follows: Given a number N, the person whose scores accumulate e…

接口测试 java_接口测试--Java

1&#xff1a;interface :定义接口接口用于 模块与模块之间的连接 或者系统与系统间的连接2&#xff1a;软件系统UI层&#xff1a;程序的界面&#xff0c;主要为用户提供交互和操作--查看&#xff0c;输入&#xff0c;点击&#xff0c;等。业务逻辑层&#xff1a;进行复杂的业…

【ZOJ - 2949】Coins of Luck (概率dp,期望)

题干&#xff1a; 给你两种泡面 各N包&#xff0c;然后你扔硬币&#xff0c;正面朝上吃第一种&#xff0c;反面呢 吃第二种&#xff0c;有任意一种吃完时 就不需要抛硬币了&#xff0c;求停止抛硬币的期望。 Sample Input 2 1 2Sample Output 1.00 2.50 解题报告&#xff…

java perl_在Java中调用Perl脚本

有两种方法&#xff0c;第一种是直接 Runtime.getRuntime().exec("...");这里推荐第二种&#xff0c;可以得到输出和返回值&#xff1a;源代码如下&#xff1a;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;public c…

*【HDU - 6201】transaction transaction transaction(树形dp 或 spfa最长路 或 网络流)

题干&#xff1a; 题目大意&#xff1a; 给出一棵n个顶点的树&#xff0c;每个点有一个权值&#xff0c;代表商品的售价&#xff0c;树上每一条边上也有一个权值&#xff0c;代表从这条边经过所需要的花费。现在需要你在树上选择两个点&#xff0c;一个作为买入商品的点&#…

java 手写签名_Android 自定义View手写签名并保存图片

GIF压缩有问题&#xff0c;运行很顺滑&#xff01;&#xff01;&#xff01;1.自定义View——支持设置画笔颜色&#xff0c;画笔宽度&#xff0c;画板颜色&#xff0c;清除画板&#xff0c;检查是否有签名&#xff0c;保存画板图片(复制粘贴可直接使用)/*** Created by YyyyQ o…

【2019第十届蓝桥杯省赛C/C++B组题解】(非官方题解)

A。 数数题。 答案&#xff1a;490 B。 26进制模拟。 答案&#xff1a;BYQ C。 类似fib数列求值&#xff0c;递推一下就好。 答案&#xff1a;4659 D。 注意两个坑点&#xff0c;一个是正整数&#xff0c;所以枚举要从1开始。第二个坑点是互不相同的&#xff0c;为了达到这…