数学难题

L i n k Link Link

luogu U75665

D e s c r i p t i o n Description Description

小明是一个初一学生,而且还是一个常居第一的天才,所以,小明不满于当前数学的难度,因此,数学老师给小明出了一道难题:
现在有 n n n个城市,有 m m m条路线,每条路线上有一个价钱,表示走这条路要花多少钱,现在小人在起点(1号点),拥有 k k k元,要求小人到终点( n n n号点)最少花费价钱, k k k元够不够用。
即使聪明如小明,也不会做这一道题,因此,小明让更聪明的你来做了!
但是老师看见是你来做,大喊道:“刷水题的请自重!”于是又加多了一个条件:
在1到n的最短路上,会有一条路被封掉,封掉的路是不可以走的,必须绕路行走

I n p u t Input Input

第一行是 n n n m m m(如题意),接下来有 m m m行,每行一个 x x x, y y y, t t t,表示从 x x x点到 y y y点要花费 t t t元,最后一个 k k k,表示小明有多少钱。

O u t p u t Output Output

若够用,请输出 " E n o u g h " "Enough" "Enough"(引号不算)
不够用的话,请输出 " N o t "Not "Not e n o u g h " enough" enough"(同上)

S a m p l e Sample Sample I n p u t Input Input

3 3
1 2 5
1 3 17
2 3 9
15

S a m p l e Sample Sample O u t p u t Output Output

Not enough

E x p l a i n Explain Explain

30% n &lt; = 100 , m &lt; = 250 , k &lt; = 1000 , t &lt; = 300 n&lt;=100,m&lt;=250,k&lt;=1000,t&lt;=300 n<=100,m<=250,k<=1000,t<=300
100% n &lt; = 1000 , m &lt; = 2500 , k &lt; = 10000 , t &lt; = 3000 n&lt;=1000,m&lt;=2500,k&lt;=10000,t&lt;=3000 n<=1000,m<=2500,k<=10000,t<=3000

T r a i n Train Train o f of of T h o u g h t Thought Thought

可以先找出最短路,然后枚举封掉最短路上的边,求最小值再比较就可以了

C o d e Code Code

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int k,tt=1,h[1005],dis[1005],minx=10086;
int n,m,t,r[1005],way[1025]; 
bool b[1005],f;
struct node
{
	int now,next,time,last;//last表示从哪个点来
}w[5005];
void SPFA(int xx)
{
	queue<int>q;
	dis[1]=0;
	b[1]=true;
	q.push(1);
	while (q.size())
	{
		int tot=q.front();
		q.pop();
		for (int i=h[tot]; i; i=w[i].next)
		{
			if (i==xx) continue;//封边
			if (dis[w[i].now]>dis[tot]+w[i].time) {
			 dis[w[i].now]=dis[tot]+w[i].time;
			 if (!f)r[w[i].now]=i;
			 if(!b[w[i].now]) 
			 {
			   b[w[i].now]=true;
			   q.push(w[i].now);
			 }
			}
		}
		b[1]=false;
	} 
}
int main()
{
	memset(dis,0x7f,sizeof(dis));
	int x,y,z;
	scanf("%d%d",&n,&m);
	for (int i=1; i<=m; ++i)
	 {
	 	scanf("%d%d%d",&x,&y,&z);
	 	w[++t]=(node){y,h[x],z,x}; h[x]=t;
	 	w[++t]=(node){x,h[y],z,y}; h[y]=t;
	 }
	scanf("%d",&k);
	SPFA(0); 
	f=true;
	tt--;
	int ttt=tt;
	for (int i=n; i!=1; i=w[r[i]].last)
	  {
	  	memset(dis,0x7f,sizeof(dis));
	  	memset(b,false,sizeof(b));
	    SPFA(r[i]);
	    if (i==0) {printf("Not enough"); break;}
	    if (dis[n]!=dis[0]) minx=min(minx,dis[n]);
	  }
	if (minx<k) printf("Enough");
	 else printf("Not enough");
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值