标签:its else typedef 注意 rhs name pen pre targe
方法一:分层图
#include<bits/stdc++.h> #define per(i,a,b) for(int i=a;i<=b;i++) #define mod 1000000007 using namespace std; typedef long long ll; const ll inf =23333333333333333LL; const double eps=1e-8; int T; int read(){ char ch=getchar(); int res=0,f=0; while(ch<‘0‘ || ch>‘9‘){f=(ch==‘-‘?-1:1);ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){res=res*10+(ch-‘0‘);ch=getchar();} return res*f; } // ------------------------head const int siz=100005; map<string,int> mp; int mcnt=0,n,m; int d; char x[15],y[15],schar[15],echar[15]; int head[siz],Enum=0; ll dis[siz][2]; bool vis[siz][2]; int st,ed; struct Edge{int to,w,ne;}edge[siz*5]; void add_edge(int a,int b,int c){ edge[Enum].to=b; edge[Enum].w=c; edge[Enum].ne=head[a]; head[a]=Enum++; } void init() { memset(head,-1,sizeof(head)); Enum=0; mp.clear(); mcnt=0; } int _hash(char *s){//char*作为实参传给string形参会自动变 if(mp.count(s)>0)return mp[s];//string作为形参传给char*要转变s.c_str(); else return mp[s]=mcnt++; } struct Qnode{ int u; ll _dis; int layer;//位于分层图的哪层 Qnode(){} Qnode(ll a,int b,int c):_dis(a),u(b),layer(c){} bool operator<(const Qnode&rhs)const{return _dis>rhs._dis;}//一定要加const }; void Dijkstra(){ priority_queue<Qnode>que; while(!que.empty())que.pop(); memset(vis,false,sizeof(vis)); per(i,0,n){dis[i][0]=inf;dis[i][1]=inf;} que.push(Qnode(0LL,st,0)); dis[st][0]=0; //注意初始化的是st点,而不是0点,这里没注意到!!! vis[st][0]=true; Qnode tmp; while(!que.empty()){ tmp=que.top();que.pop(); int u=tmp.u,layer=tmp.layer; ll _dis=tmp._dis; //printf("dis:%lld u:%d layer:%d\n",_dis,u,layer);// vis[u][layer]=true; //if(u==ed && layer==1)return;// //printf("dis[%d]:%lld\n",u,dis[u][0]);// for(int i=head[u];i!=-1;i=edge[i].ne){ int v=edge[i].to,w=edge[i].w; //printf("w:%d\n",w);exit(0);// //printf("v:%d \n");// //printf("dis[u][layer]:%lld\n",dis[u][layer]);// //printf("dis[v][layer+1]:%lld dis[v][layer]:%lld w:%d\n",dis[v][layer+1],dis[v][layer],w);// //if(dis[v][layer+1]>dis[u][layer]+w/2)printf("cas1 ok\n");else printf("cas1 no\n");// if(layer<1 && !vis[v][layer+1] &&dis[v][layer+1]>dis[u][layer]+w/2){ dis[v][layer+1]=dis[u][layer]+w/2; //printf("Push v%d dis:%lld layer:%d\n",v,dis[v][layer],layer+1);// que.push(Qnode(dis[v][layer+1],v,layer+1)); } if(!vis[v][layer] && dis[v][layer]>dis[u][layer]+w){ dis[v][layer]=dis[u][layer]+w; que.push(Qnode(dis[v][layer],v,layer)); } } } } int main() { while(scanf("%d %d",&n,&m)!=EOF){ init(); per(i,1,m){ scanf("%s %s %d",x,y,&d); int xn=_hash(x),yn=_hash(y); //printf("%s:%d %s:%d d:%d\n",x,xn,y,yn,d);// add_edge(xn,yn,d); } scanf("%s %s",schar,echar); if(mp.count(schar)==0||mp.count(echar)==0){printf("-1\n");continue;} st=_hash(schar);ed=_hash(echar); //printf("%s:%d %s:%d\n",schar,st,echar,ed);// Dijkstra(); ll ans=min(dis[ed][0],dis[ed][1]); if(ans==inf)printf("-1\n"); else printf("%lld\n",ans); } return 0; }
方法二:反向建图
标签:its else typedef 注意 rhs name pen pre targe
原文地址:https://www.cnblogs.com/WindFreedom/p/9597556.html