L2-001 紧急救援
2019-03-16 18:59
81 查看
总结: 用了priority_queue<E,vector<E>,greater<E> >错了
[code]#include<algorithm> #include<set> #include<vector> #include<queue> #include<cmath> #include<cstring> #include<iostream> #include<algorithm> #include<set> #include<vector> #include<queue> #include<cmath> #include<cstring> #include<sstream> #include<cstdio> #include<ctime> #include<map> #include<stack> #include<string> using namespace std; #define sfi(i) scanf("%d",&i) #define pri(i) printf("%d\n",i) #define sff(i) scanf("%lf",&i) #define ll long long #define mem(x,y) memset(x,y,sizeof(x)) #define INF 0x3f3f3f3f #define eps 1e-6 #define PI acos(-1) #define lowbit(x) ((x)&(-x)) #define zero(x) (((x)>0?(x):-(x))<eps) #define fl() printf("flag\n") ll gcd(ll a,ll b){while(b^=a^=b^=a%=b);return a;} const int maxn=1e4+9; const int mod=1e9+7; template <class T> inline void sc(T &ret) { char c; ret = 0; while ((c = getchar()) < '0' || c > '9'); while (c >= '0' && c <= '9') { ret = ret * 10 + (c - '0'), c = getchar(); } } ll power(ll n,ll x) { ll ans=1; while(n) { if(n&1) ans=ans*x; n>>=1; x=x*x; } return ans; } struct E { int to,cost; E(int _to,int _cost) { to=_to; cost=_cost; } bool operator<(const E &b) const { return cost>b.cost; } }; vector<E> g[maxn]; vector<int>path; int d[maxn],cot[maxn],pre[maxn],w[maxn],m,n,s,e,num[maxn]; bool vis[maxn]; priority_queue<E>que; void dj(int s) { mem(vis,0); que.push(E(s,0)); for(int i=0;i<n;i++) d[i]=INF,cot[i]=-INF,num[i]=0; cot[s]=w[s]; d[s]=0; num[s]=1; pre[s]=-1; while(!que.empty()) { E t=que.top(); que.pop(); int u=t.to; if(vis[u])continue; vis[u]=1; //cout<<u<<' '; int ct=t.cost; for(int i=0;i<g[u].size();i++) { int v=g[u][i].to; //cout<<v<<' '; int ctt=g[u][i].cost; if(d[v]>d[u]+ctt) { num[v]=num[u]; d[v]=d[u]+ctt; cot[v]=cot[u]+w[v]; pre[v]=u; que.push(E(v,d[v])); } else if(d[v]==d[u]+ctt) { num[v]+=num[u]; if(cot[v]<cot[u]+w[v]) { cot[v]=cot[u]+w[v]; pre[v]=u; que.push(E(v,d[v])); } } } } } int main() { cin>>n>>m>>s>>e; //mem(pre,-1); for(int i=0;i<n;i++) { cin>>w[i]; } for(int i=0;i<m;i++) { int u,v,cost; cin>>u>>v>>cost; g[u].push_back(E(v,cost)); g[v].push_back(E(u,cost)); } dj(s); for(int i=e;i!=-1;i=pre[i]) { path.push_back(i); } reverse(path.begin(),path.end()); cout<<num[e]<<" "<<cot[e]<<endl; for(int i=0;i<path.size();i++) { if(i!=0) cout<<" "; cout<<path[i]; } cout<<endl; return 0; }
相关文章推荐
- PAT L2-001. 紧急救援
- L2-001. 紧急救援Dijkstra算法(单源最短路径)
- L2-001. 紧急救援
- 天梯赛L2-001紧急救援
- PAT L2-001. 紧急救援 Dijkstra+权值+路径记录
- L2-001. 紧急救援
- PAT L2-001. 紧急救援 Dijkstra+权值+路径记录
- 团体程序设计天梯赛-练习集 L2-001. 紧急救援
- 团体程序设计天梯赛-练习集 L2-001. 紧急救援
- PAT L2——001紧急救援
- PAT L2-001. 紧急救援 Dijkstra+权值+路径记录
- 天梯赛 L2-001. 紧急救援
- PAT - 天梯赛 L2-001 紧急救援
- PAT L2-001. 紧急救援 Dijkstra+权值+路径记录
- 天梯-L2-001. 紧急救援
- PAT L2-001. 紧急救援 Dijkstra+权值+路径记录
- L2-001. 紧急救援
- PAT L2-001. 紧急救援 Dijkstra+权值+路径记录
- PTA L2-001 紧急救援
- PAT L2-001. 紧急救援 Dijkstra+权值+路径记录