【spfa】Breakthrough

Problem Description

Mr. Garantou is playing a very interesting game. There are at most 100 maps in this game, of which map 1 is the starting point and room n is the ending point. Some maps are supply stations, which can add ki points of physical strength, while there are monsters in some maps, which need to consume ki points of physical strength, and there are some one-way channel links between maps.
Garantou-kun starts from map 1 and has 100 initial stamina. Each time you enter a map, you need to deduct or increase the corresponding physical value. This process continues until it reaches the end, or when the stamina value returns to zero, it will be Game Over. However, he can pass through the same map any number of times, and each time he needs to accept the physical value of the map.
Input format
Line 1 An integer n (n≤100).
Lines 2 ~ n+1, the first integer in each line represents the change in the physical value of the map. Next is a list of rooms that can be reached from this room, the first integer is the number of rooms, followed by the number of rooms that can be reached.
Output format
If the player can reach the end, output Yes, otherwise output No.
Sample input
5
0 1 2
-60 1 3
-60 1 4
20 1 5
0 0
Sample output

No


General idea:

Because of the negative power, I plan to use spfa.

The idea is to find the longest path from the source point node 1 to the end point , and see if the maximum physical strength value to the end point is less than 0, otherwise it cannot be reached. Note that the source point's dis[1] is set to 100.

The difference between this and the spfa template is that this is not weighted by side, but weighted by point , so the weight w is not defined in the struct of the side, but an array is set up to store w.

Code:

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
const int MAX_N=105;
const int MAX_M=10000;
struct
{
	int e;int next;
}e[MAX_M];
int p[MAX_N]; //Remember to initialize to -1
int cnt=0;
int w[MAX_N]; //According to the meaning of this question, the weight is not the opposite side, so w is not written in e
void add(int ss,int ee)
{
	e[cnt].e=ee;
	//e[cnt].w=w;
	e[cnt].next=p[ss];
	p[ss]=cnt;
	cnt++;
}
void add2(int s,int e)
{
	add(s,e);
	add(e,s);
}
// find the longest path
int dis[MAX_N]; //Be careful to initialize to -inf first!
bool isin[MAX_N]; //The tag array of the queue
queue<int> q;
void spfa_MAX(int s)
{
	dis[s]=w[s]+100; //According to the meaning of the question, there is initial physical strength just starting
	isin[s]=true;
	q.push(s);
	while(q.empty()==false)
	{
		int x=q.front();
		q.pop();
		isin[x]=false;
		for(int i=p[x];i!=-1;i=e[i].next)
		{
			int y = e [i] .e;
			if(dis[x]+w[y]>dis[y] && dis[x]+w[y]>0)
			{
				dis[y]=dis[x]+w[y];
				if(isin[y]==false)
				{
					isin[y]=true;
					q.push(y);
				}
			}
		}
	}
}

intmain()
{
	memset(p,-1,sizeof(p));
	memset(dis,-1e6,sizeof(dis));
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int a;
		cin>>a;
		w[i]=a;
		int k;
		cin>>k;
		while(k--)
		{
			int b;
			cin>>b;
			add2(i,b);
		}
	}
	spfa_MAX(1);
	if(dis[n]<0) cout<<"No";
	else	cout<<"Yes";
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324520374&siteId=291194637