pat 甲1003. Emergency(最短路)
2016-11-16 17:12
218 查看
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.
Sample Input
5 6 0 2 1 2 1 5 3 0 1 1 0 2 2 0 3 1 1 2 1 2 4 1 3 4 1
Sample Output
2 4
tip:题目的意思是求出一个有权无向图的最小路径个数以及定点权值和最大值。
细心细心细心,一开始book数组开小了...导致测试点老不过,调了半天。
#include<iostream> #include<cstring> using namespace std; int map[510][510]; int n,m,s,e,maxn;//人数最大值 int p[510]; int book[510]; int ans,minn=0x7f7f7f7f;//最短路数量,最短路大小 void dfs(int step,int cnt,int sum) { if(step==e){ if(sum<minn) { ans=1; minn=sum; maxn=cnt; } else if(sum==minn){ ans++; maxn=max(maxn,cnt); } return; } for(int i=0;i<n;i++){ if(map[step][i]&&!book[i]){ book[i]=1; dfs(i,cnt+p[i],sum+map[step][i]); book[i]=0; } } } int main() { cin>>n>>m>>s>>e; for(int i=0;i<n;i++)cin>>p[i]; for(int i=1;i<=m;i++){ int x,y,z;cin>>x>>y>>z; map[x][y]=z;map[y][x]=z; } book[s]=1; dfs(s,p[s],0); cout<<ans<<" "<<maxn<<endl; return 0; }
方法二:迪杰斯特拉算法:
#include<iostream> #include<cstring> #include<vector> #include<queue> using namespace std; int n,m,s,e; int p[1100]; int map[1100][1100]; vector<int>g[1100]; const int inf=0x3f3f3f3f; int book[1100],dis[1100]; int sum[1100],cnt[1100]; void dijkstra() { memset(book,0,sizeof(book)); memset(dis,0x3f,sizeof(dis)); dis[s]=0; cnt[s]=1; sum[s]=p[s]; while(1) { int index,minn=inf; for(int i=0;i<n;i++) { if(!book[i]&&dis[i]<minn) { minn=dis[i],index=i; } } if(minn==inf)break; book[index]=1; for(int i=0;i<g[index].size();i++) { int nx=g[index][i]; if(dis[nx]>dis[index]+map[index][nx]) { dis[nx]=dis[index]+map[index][nx]; cnt[nx]=cnt[index]; sum[nx]=sum[index]+p[nx]; } else if(dis[nx]==dis[index]+map[index][nx]) { cnt[nx]+=cnt[index]; sum[nx]=max(sum[nx],sum[index]+p[nx]); } } } } int main() { cin>>n>>m>>s>>e; for(int i=0;i<n;i++)cin>>p[i]; for(int i=1;i<=m;i++) { int x,y,z;cin>>x>>y>>z; g[x].push_back(y);g[y].push_back(x); map[x][y]=map[y][x]=z; } dijkstra(); cout<<cnt[e]<<" "<<sum[e]<<endl; return 0; }
法三:堆优化的dijkstra()
#include<iostream> #include<cstring> #include<vector> #include<queue> using namespace std; int n,m,s,e; int p[1100]; int map[1100][1100]; vector<int>g[1100]; const int inf=0x3f3f3f3f; int book[1100],dis[1100]; int sum[1100],cnt[1100]; struct node{int x,d;friend bool operator <(node n1,node n2){return n1.d>n2.d;}}; void dijkstra() { memset(book,0,sizeof(book)); memset(dis,0x3f,sizeof(dis)); dis[s]=0; cnt[s]=1; sum[s]=p[s]; priority_queue<node>q; q.push(node{s,0}); while(!q.empty()) { node tmp=q.top();q.pop(); int x=tmp.x; if(book[x])continue;book[x]=1; for(int i=0;i<g[x].size();i++) { int nx=g[x][i]; if(dis[nx]>dis[x]+map[x][nx]) { dis[nx]=dis[x]+map[x][nx]; q.push(node{nx,dis[nx]}); cnt[nx]=cnt[x]; sum[nx]=sum[x]+p[nx]; } else if(dis[nx]==dis[x]+map[x][nx]) { cnt[nx]+=cnt[x]; sum[nx]=max(sum[nx],sum[x]+p[nx]); } } } } int main() { cin>>n>>m>>s>>e; for(int i=0;i<n;i++)cin>>p[i]; for(int i=1;i<=m;i++) { int x,y,z;cin>>x>>y>>z; g[x].push_back(y);g[y].push_back(x); map[x][y]=map[y][x]=z; } dijkstra(); cout<<cnt[e]<<" "<<sum[e]<<endl; return 0; }
相关文章推荐
- PAT 1003. Emergency 单源最短路
- PAT 1003. Emergency Dijkstra变形+求相等最短路的数量+特殊权重
- PAT甲级-1003. Emergency (25)多条最短路
- PAT--1003. Emergency (最短路)
- PAT(Advanced Level) 1003. Emergency(25) 最短路 + DFS
- PAT 1003. Emergency (25) (求两点间最短路的条数)
- PAT (Advanced Level) Practise 1003. Emergency (25) Dijstra扩展应用
- PAT 甲级1003. Emergency (25) DIJKSTRA
- PAT 1003. Emergency (25)
- PAT 1003. Emergency (25) Dijkstra算法 + DFS
- 1003. Emergency (25)——PAT (Advanced Level) Practise
- pat 1003. Emergency (25)
- PAT 1003. Emergency (25)
- PAT-A 1003. Emergency (25)
- PAT(A)1003 Emergency(最短路+计数)
- PAT 1003. Emergency (25)---深搜
- PAT-A-1003. Emergency (25)
- PAT 1018 Public Bike Management(Dijkstra 最短路)
- PAT TEST甲级1003. Emergency (25)
- pat 1003. Emergency (25)