Gym - 101986F Pizza Delivery (最短路必经路径)

 

题意:给你一个有向图,每一条边在第i天都会反向,问反向后的最短路是否有变化。每次反向都是独立的。

 

解题思路:我们先把起点到所有点的最短路求出来,然后把所有边反向,然后求终点到所有点的最短路。

这样我们就记录了两个数组d1,d2,分别记录起点和终点到所有点的最短路。然后用s记录刚开始的最短路。当我们反向一条边的时候只需要判断 d1[v]+d2[u]+w<s,如果小于s,那么肯定是happy,否则可能是sad也可能是soso,这时只要判断这条边是否是最短路的必经边即可,如果是必经边,肯定是sad,否则就是soso。

那么怎么判断是否是必经边呢?只要枚举每一条边,把每一条边反向,看看最短路是否会发生变化,不会发生变化的话,就有可能是必经边,把他变成无向边,放到一个图里,最后对这个图求桥,求出的桥都是必经边,其他的都是非必经边。

 

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=2e5+5;
struct node{
	int nxt,u,v,w;
};
bool bi[maxn];//保存是否是必经边
struct Graph{
	struct Node{
		int v;
		long long dis;
		Node (long long a,int b):dis(a),v(b){};
		bool operator<(const Node &a)const{
			return dis>a.dis;
		}
	};
	node edge[maxn];
	int tot=0;
	int head[maxn],id[maxn];
	bool vis[maxn];
	long long d[maxn];
	Graph(){
		memset(head,-1,sizeof(head));
	}
	void add(int u,int v,int w){
		edge[tot]={head[u],u,v,w};
		head[u]=tot++;
	}
	void add(int u,int v,int w,int _id){
		edge[tot]={head[u],u,v,w};
		id[tot]=_id;head[u]=tot++;
	}
	void dijkstra(int S){
		priority_queue<Node>Q;
		memset(d,0x3f,sizeof(d));
		memset(vis,0,sizeof(vis));
		d[S]=0;
		Q.push(Node(d[S],S));
		while (!Q.empty()){
			Node t=Q.top();
			Q.pop();
			int v=t.v;
			if (vis[v]) continue;
			vis[v]=1;
			for (int i=head[v];~i;i=edge[i].nxt){
				if (d[edge[i].v]>d[v]+edge[i].w){
					d[edge[i].v]=d[v]+edge[i].w;
					Q.push(Node(d[edge[i].v],edge[i].v));
				}
			}
		}
	}
	int vistime=0;
	int dfn[maxn],low[maxn];
	void tarjan(int v,int u){
		dfn[v]=low[v]=++vistime;
		for (int i=head[v];~i;i=edge[i].nxt){
			if (!dfn[edge[i].v]){
				tarjan(edge[i].v,v);
				low[v]=min(low[v],low[edge[i].v]);
				if (low[edge[i].v]>dfn[v])
					bi[id[i]]=1;//桥肯定是必经边
			}
			else
				if (edge[i].v!=u)
					low[v]=min(low[v],dfn[edge[i].v]);
		}
	}
}G1,G2,G3;
int n,m,u,v,w;
int main(){
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++){
		scanf("%d%d%d",&u,&v,&w);
		G1.add(u,v,w);
		G2.add(v,u,w);
	}
	G1.dijkstra(1);
	G2.dijkstra(2);
	long long s=G1.d[2];
	for (int i=0;i<G1.tot;i++){
		u=G1.edge[i].u;
		v=G1.edge[i].v;
		w=G1.edge[i].w;
		if (G1.d[u]+G2.d[v]+w==s){
			G3.add(u,v,w,i);
			G3.add(v,u,w,i);
		}
	}
	G3.tarjan(1,0);
	for (int i=0;i<G1.tot;i++){
		u=G1.edge[i].u;
		v=G1.edge[i].v;
		w=G1.edge[i].w;
		if (G1.d[v]+G2.d[u]+w<s){
			printf("HAPPY\n");
		}else if (bi[i])
			printf("SAD\n");
		else printf("SOSO\n");
	}
    return 0;
} 

.

 

 

 

 

 

 

发布了402 篇原创文章 · 获赞 67 · 访问量 16万+
展开阅读全文

Pizza delivery

12-27

Description Your Irish pizza and kebab restaurant is doing very well. Not only is the restaurant full almost every night, but there is also an ever increasing number of deliveries to be made, all over town. To meet this demand, you realize that it will be necessary to separate the delivery service from the restaurant. A new large kitchen, only for baking pizzas and being a base for deliveries, has to be established somewhere in town. The main cost in the delivery service is not the making of the pizza itself, but the time it takes to deliver it. To minimize this, you need to carefully plan the location of the new kitchen. To your help you have a database of all last year's deliveries. For each block in the city, you know how many deliveries were made there last year. The kitchen location will be chosen based on the assumption that the pattern of demand will be the same in the future. Your city has a typical suburban layot - an orthogonal grid of equalsize square blocks. All places of interest (delivery points and the kitchen) are considered to be located at street crossings. The distance between two street crossings is the Manhattan distance, i.e., the number of blocks you have to drive vertically, plus the number of blocks you have to drive horizontally. The total cost for a delivery point is its Manhattan distance from the kitchen, times the number of deliveries to the point. Note that we are only counting the distance from the kitchen to the delivery point. Even though we always drive directly back to the kitchen after a delivery is made, this (equal) distance is not included in the cost measure. Input On the first line, there is a number, 1 ≤ n ≤ 20, indicating the number of test cases. Each test case begins with a line with two integers, 1 ≤ x ≤ 100, 1 ≤ y ≤ 100, indicating the size of the two-dimenstional street grid. Then follow y lines, each with x integers, 0 ≤ d ≤ 1000, indicating the number of deliveries made to each street crossing last year. Output For each test case, output the least possible total delivery cost (the sum of all delivery costs last year), assuming that the kitchen was located optimally. There should be one line for each test case, with an integer indicating the cost, followed by a single space and the word 'blocks'. Sample Input 2 4 4 0 8 2 0 1 4 5 0 0 1 0 1 3 9 2 0 6 7 0 0 0 0 0 0 0 1 0 3 0 1 2 9 1 2 1 2 8 7 1 3 4 3 1 0 2 2 7 7 0 1 0 0 1 0 0 0 0 0 0 0 Sample Output 55 blocks 162 blocks 问答

Pizza delivery

05-27

Problem Description Your Irish pizza and kebab restaurant is doing very well. Not only is the restaurant full almost every night, but there is also an ever increasing number of deliveries to be made, all over town. To meet this demand, you realize that it will be necessary to separate the delivery service from the restaurant. A new large kitchen, only for baking pizzas and being a base for deliveries, has to be established somewhere in town. The main cost in the delivery service is not the making of the pizza itself, but the time it takes to deliver it. To minimize this, you need to carefully plan the location of the new kitchen. To your help you have a database of all last year's deliveries. For each block in the city, you know how many deliveries were made there last year. The kitchen location will be chosen based on the assumption that the pattern of demand will be the same in the future. Your city has a typical suburban layot – an orthogonal grid of equalsize square blocks. All places of interest (delivery points and the kitchen) are considered to be located at street crossings. The distance between two street crossings is the Manhattan distance, i.e., the number of blocks you have to drive vertically, plus the number of blocks you have to drive horizontally. The total cost for a delivery point is its Manhattan distance from the kitchen, times the number of deliveries to the point. Note that we are only counting the distance from the kitchen to the delivery point. Even though we always drive directly back to the kitchen after a delivery is made, this (equal) distance is not included in the cost measure. Input On the rst line, there is a number, 1 <=n <=20, indicating the number of test cases. Each test case begins with a line with two integers, 1 <= x <= 100, 1<= y <= 100, indicating the size of the two-dimenstional street grid. Then follow y lines, each with x integers, 0<= d <=1000, indicating the number of deliveries made to each street crossing last year. Output For each test case, output the least possible total delivery cost (the sum of all delivery costs last year), assuming that the kitchen was located optimally.There should be one line for each test case, with an integer indicating the cost, followed by a single space and the word 'blocks'. Sample Input 2 4 4 0 8 2 0 1 4 5 0 0 1 0 1 3 9 2 0 6 7 0 0 0 0 0 0 0 1 0 3 0 1 2 9 1 2 1 2 8 7 1 3 4 3 1 0 2 2 7 7 0 1 0 0 1 0 0 0 0 0 0 0 Sample Output 55 blocks 162 blocks 问答

Pizza delivery

06-27

Problem Description Your Irish pizza and kebab restaurant is doing very well. Not only is the restaurant full almost every night, but there is also an ever increasing number of deliveries to be made, all over town. To meet this demand, you realize that it will be necessary to separate the delivery service from the restaurant. A new large kitchen, only for baking pizzas and being a base for deliveries, has to be established somewhere in town. The main cost in the delivery service is not the making of the pizza itself, but the time it takes to deliver it. To minimize this, you need to carefully plan the location of the new kitchen. To your help you have a database of all last year's deliveries. For each block in the city, you know how many deliveries were made there last year. The kitchen location will be chosen based on the assumption that the pattern of demand will be the same in the future. Your city has a typical suburban layot – an orthogonal grid of equalsize square blocks. All places of interest (delivery points and the kitchen) are considered to be located at street crossings. The distance between two street crossings is the Manhattan distance, i.e., the number of blocks you have to drive vertically, plus the number of blocks you have to drive horizontally. The total cost for a delivery point is its Manhattan distance from the kitchen, times the number of deliveries to the point. Note that we are only counting the distance from the kitchen to the delivery point. Even though we always drive directly back to the kitchen after a delivery is made, this (equal) distance is not included in the cost measure. Input On the rst line, there is a number, 1 <=n <=20, indicating the number of test cases. Each test case begins with a line with two integers, 1 <= x <= 100, 1<= y <= 100, indicating the size of the two-dimenstional street grid. Then follow y lines, each with x integers, 0<= d <=1000, indicating the number of deliveries made to each street crossing last year. Output For each test case, output the least possible total delivery cost (the sum of all delivery costs last year), assuming that the kitchen was located optimally.There should be one line for each test case, with an integer indicating the cost, followed by a single space and the word 'blocks'. Sample Input 2 4 4 0 8 2 0 1 4 5 0 0 1 0 1 3 9 2 0 6 7 0 0 0 0 0 0 0 1 0 3 0 1 2 9 1 2 1 2 8 7 1 3 4 3 1 0 2 2 7 7 0 1 0 0 1 0 0 0 0 0 0 0 Sample Output 55 blocks 162 blocks 问答

Pizza delivery

08-24

Description Your Irish pizza and kebab restaurant is doing very well. Not only is the restaurant full almost every night, but there is also an ever increasing number of deliveries to be made, all over town. To meet this demand, you realize that it will be necessary to separate the delivery service from the restaurant. A new large kitchen, only for baking pizzas and being a base for deliveries, has to be established somewhere in town. The main cost in the delivery service is not the making of the pizza itself, but the time it takes to deliver it. To minimize this, you need to carefully plan the location of the new kitchen. To your help you have a database of all last year's deliveries. For each block in the city, you know how many deliveries were made there last year. The kitchen location will be chosen based on the assumption that the pattern of demand will be the same in the future. Your city has a typical suburban layot - an orthogonal grid of equalsize square blocks. All places of interest (delivery points and the kitchen) are considered to be located at street crossings. The distance between two street crossings is the Manhattan distance, i.e., the number of blocks you have to drive vertically, plus the number of blocks you have to drive horizontally. The total cost for a delivery point is its Manhattan distance from the kitchen, times the number of deliveries to the point. Note that we are only counting the distance from the kitchen to the delivery point. Even though we always drive directly back to the kitchen after a delivery is made, this (equal) distance is not included in the cost measure. Input On the first line, there is a number, 1 ≤ n ≤ 20, indicating the number of test cases. Each test case begins with a line with two integers, 1 ≤ x ≤ 100, 1 ≤ y ≤ 100, indicating the size of the two-dimenstional street grid. Then follow y lines, each with x integers, 0 ≤ d ≤ 1000, indicating the number of deliveries made to each street crossing last year. Output For each test case, output the least possible total delivery cost (the sum of all delivery costs last year), assuming that the kitchen was located optimally. There should be one line for each test case, with an integer indicating the cost, followed by a single space and the word 'blocks'. Sample Input 2 4 4 0 8 2 0 1 4 5 0 0 1 0 1 3 9 2 0 6 7 0 0 0 0 0 0 0 1 0 3 0 1 2 9 1 2 1 2 8 7 1 3 4 3 1 0 2 2 7 7 0 1 0 0 1 0 0 0 0 0 0 0 Sample Output 55 blocks 162 blocks 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 点我我会动 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览