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;
}
#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;
}
相关文章推荐
- pat1003 Emergency
- 1003. Emergency (25)-PAT
- pat1003. Emergency
- pat 甲级 1003 Emergency
- PAT 1003 Emergency
- 【PAT】甲级1003 - Emergency(最短路)
- PAT(A)1003 Emergency(最短路+计数)
- PAT 1003 Emergency
- 【最短路】PAT 1003 Emergency(求最短路条数 和 第二关键词最大)
- PAT 1003 Emergency(最短路(迪杰斯特拉||贝尔曼)最小边权下的最大点权)
- PAT (Advanced Level) Practise 1003 Emergency
- PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)
- PAT1003. Emergency (25)
- PAT Advanced Level 1003:Emergency
- PAT (Advanced Level) 1003 - Emergency(单元最短路)
- [PAT-甲级]1003.Emergency
- PAT1003. Emergency (25) Dijkstra
- 【PAT 1003】 Emergency 图论Dijkstra
- Pat 1003 Emergency (25)
- PAT 甲级试题1003 Emergency (25) dijkstra变形应用