PAT (Advanced) 1003

6 篇文章 0 订阅

简单的图 

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int map[505][505];
int teams[505];
int vis[505];
int n, m, start, endc;
int INF = 100000;
int pmin = INF, path = 0, tmax = 0;

void solve(int len, int x, int team){
	if(x == endc){
		if(len < pmin){
			pmin = len;
			path = 1;
			tmax = team;
		}
		else if(len == pmin){
			path++;
			tmax = max(team, tmax);
		}
		return;
	}
	for(int i = 0; i < n; i++){
		if(vis[i] == 0 && map[x][i] < INF && len + map[x][i] <= pmin){
			vis[i] = 1;
			
			solve(len + map[x][i], i, team+teams[i]);
			vis[i] = 0;
		}
	}
}

int main(){
	scanf("%d%d%d%d",&n,&m,&start,&endc);
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++)
			map[i][j] = INF;
	}
	memset(vis, 0, n);
	for(int i = 0; i < n; i++)
		scanf("%d",&teams[i]);
	for(int i = 0; i < m; i++){
		int x, y, v;
		scanf("%d%d%d",&x,&y,&v);
		map[x][y] = map[y][x] = v;
	}
	vis[start] = 1;
	solve(0, start, teams[start]);
	printf("%d %d", path, tmax);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值