PAT 1003. Emergency (25)---深搜
2016-05-08 22:42
381 查看
1003. Emergency (25)
时间限制400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked
on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.
Input
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently
in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected
by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.
Output
For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.
#include <iostream> using namespace std; const int INFINITY=1000; int n,m,loc,dtm; int* mark; int* teams; int** map; int mindis; int maxtem; int num; void dfs(int str,int end,int dis,int teamsum){ if(dis>INFINITY)return; if(str==end){ if(dis<mindis){ num=1; mindis=dis; maxtem=teamsum; }else if(dis==mindis){ num++; if(teamsum>maxtem) maxtem=teamsum; } return; } for(int i=0;i<n;i++){ if(mark[i]==0&&map[str][i]<INFINITY){ mark[i]=1; dfs(i,end,dis+map[str][i],teamsum+teams[i]); mark[i]=0; } } } int main(){ cin>>n>>m>>loc>>dtm; teams=new int ; mark=new int ; map=new int* ; for(int i=0;i<n;i++) { cin>>teams[i]; mark[i]=0; map[i]=new int ; for(int j=0;j<n;j++){ map[i][j]=INFINITY; } } for(int j=0;j<m;j++){ int a,b,x; cin>>a>>b>>x; map[a][b]=map[b][a]=x; } mindis=INFINITY; dfs(loc,dtm,0,teams[loc]); cout<<num<<" "<<maxtem; //system("pause"); return 0; }最后一个测试点超时。未解决。
相关文章推荐
- 利用sort对结构体数组进行排序
- 费波纳茨数列的递归及坑点
- 20150411--Dede二次开发-01
- buildroot(一)
- 数据库1 关系(relation) 元组(tuple) join
- HTML5列表
- 路由器与交换机的区别
- AndroidStudio中导入module(超详细)
- 设置linux开发板上电自动设置网络配置
- c++拷贝构造函数遇上等号重载
- 第10周学习进度条
- 微信入坑第一弹【微信接口配置】
- 小数化分数2
- 《java入门第一季》之面向对象静态内部类和非静态内部类的区别
- 未来智能互联汽车长啥样
- 《java入门第一季》之面向对象静态内部类和非静态内部类的区别
- android tv gridview焦点放大效果
- 各种排序算法的分析及java实现
- java代码复制到word时颜色消失的解决办法
- js判断IE6及以下版本浏览器