您的位置:首页 > 其它

ZJU PAT 1003 Emergency

2014-11-06 21:27 387 查看
最短路径,DFS 、路径非负的时候还能用dijkstra算法。

#include<iostream>
using namespace std;
inline int max_(int &a, int &b)
{
return a>b?a:b;
}
const int src = 505;
const int des = 505;
int road[src][des];//-1表不通
bool vis[src];//1表未遍历过
int hper_[src];
int len,hper,shortest,res_hper,road_num;
void dfs(int const &c1, int const &c2,int const &city_num_);
int main()
{
int city_num,city_src,city_des,i,j;
scanf("%d%d%d%d",&city_num,&road_num,&city_src,&city_des);
for(i = 0 ; i < city_num ; i++)
{
vis[i] = 1;
cin >> hper_[i];
for(j = 0 ; j < city_num; j++)road[i][j] = -1;
}
for(i = 0 ; i < road_num ; i++)
{
int tmp1,tmp2,tmp3;
cin>>tmp1>>tmp2>>tmp3;
road[tmp1][tmp2] = road[tmp2][tmp1] = tmp3;
}

shortest = -1;
len = hper = res_hper = road_num = 0;
dfs(city_src,city_des,city_num);
cout<<road_num<<" "<<res_hper<<endl;
return 0;
}
void dfs(int const &c1,int const &c2 ,int const &city_num_)
{
hper += hper_[c1];
vis[c1] = 0;
if(c1 == c2)
{
if(len <= shortest || shortest == -1)
{
if(shortest == len)
{
res_hper = max_(hper,res_hper);
road_num++;
}
else
{
shortest = len;
res_hper = hper;
road_num = 1;
}
}
return;
}
for(int c = 0 ; c < city_num_ ; c++)
{
if(vis[c]&&road[c1][c]!=-1)
{
len += road[c1][c];
dfs(c,c2,city_num_);
len -= road[c1][c];
hper -= hper_[c];
vis[c] = 1;
}
}
return;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: