PAT 1003. Emergency (25)
2014-10-29 10:43
330 查看
#include<iostream> #include<vector> using namespace std; int n, m, C1, C2; vector<int> team, path; int road[501][501] = { 0 };//如果为0是该两点间没有路 int len = 0; int vst[501] = { 0 }; int helpers = 0, minpath = -1, cnt = 0, maxhelpers = 0; void dfs(int v){ vst[v] = 1; if (v == C2) { // path.push_back(v); len = 0; helpers = team[C1]; for (int i = 0; i < path.size() - 1;){ int c1 = path[i], c2 = path[++i]; len += road[c1][c2]; helpers += team[c2]; } if (minpath == -1) { minpath = len; maxhelpers = helpers; cnt = 1; } else if (len < minpath){ minpath = len; cnt = 1; maxhelpers = helpers; } else if (len == minpath){ cnt++; if (maxhelpers < helpers) maxhelpers = helpers; } return; } else{ for (int i = 0; i < n; i++){ if (!vst[i] && road[v][i] != 0){ path.push_back(i); dfs(i); path.pop_back(); vst[i] = 0; } } } return; } int main(){ cin >> n >> m >> C1 >> C2; for (int i = 0; i < n; i++){ int person; cin >> person; team.push_back(person); } for (int i = 0; i < m; i++){ int c1, c2, l; cin >> c1 >> c2 >> l; road[c1][c2] = road[c2][c1] = l; } path.push_back(C1); dfs(C1); cout << cnt << " " << maxhelpers; }
相关文章推荐
- 【PAT】1003. Emergency (25)
- 【学习笔记】〖PAT〗1003. Emergency (25)
- PAT 1003. Emergency (25)
- PAT-A-1003. Emergency (25)
- PAT-A 1003. Emergency (25)
- pat-a1003. Emergency (25)
- PAT 1003. Emergency (25)---深搜
- PAT 1003. Emergency (25) 【精华】
- PAT (Advanced Level) 1003. Emergency (25) 最短路径 DFS
- PAT(甲)1003. Emergency(25)
- PAT 1003. Emergency (25)
- pat 1003. Emergency (25)
- PAT (Advanced Level) Practise 1003. Emergency (25)
- PAT 甲级 1003. Emergency (25)
- PAT(甲)1003. Emergency(25)
- PAT --- 1003. Emergency (25)
- PAT (Advanced Level) 1003. Emergency (25) 解题报告
- Pat甲级1003. Emergency (25)
- PAT(A) 1003. Emergency (25)
- PAT(甲)1003. Emergency(25)