标签:运算 字典序 using 答案 char ++i algorithm 为什么 字符
4 5
1 2 2
3 4 2
2 4 4
1 3 1
2 3 1
1 4
1 2
2
2 violethill
1 pink
violethill
4
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 #define maxn 10005 7 #define maxm 100005 8 #define inf 1<<29 9 int n,m,ecnt,ans=inf,num; 10 int pos[maxn],vis[maxn*2],dis[maxn*2],head[maxn*2]; 11 int odd[maxm],even[maxm],x[maxm],y[maxm],z[maxm]; 12 char name[maxn][20],ansch[20]; 13 struct edge{ 14 int u,v,w,next; 15 }E[maxm*2]; 16 inline int read() 17 { 18 int ret(0); 19 char ch=getchar(); 20 while(ch<‘0‘||ch>‘9‘)ch=getchar(); 21 while(ch>=‘0‘&&ch<=‘9‘) 22 { 23 ret=ret*10+ch-‘0‘; 24 ch=getchar(); 25 } 26 return ret; 27 } 28 void addedge(int u,int v,int w) 29 { 30 E[++ecnt].u=u; 31 E[ecnt].v=v; 32 E[ecnt].w=w; 33 E[ecnt].next=head[u]; 34 head[u]=ecnt; 35 } 36 void add() 37 { 38 for(int i=1 ; i<=m ; ++i ) 39 { 40 if(odd[i])addedge(y[i],x[i]+n,z[i]); 41 if(even[i])addedge(y[i]+n,x[i],z[i]); 42 if((odd[i]|even[i])==0) 43 { 44 addedge(y[i],x[i]+n,z[i]); 45 addedge(y[i]+n,x[i],z[i]); 46 } 47 } 48 } 49 void spfa() 50 { 51 queue<int> q; 52 memset(dis,127/3,sizeof(dis)); 53 vis[n]=vis[n<<1]=1; 54 dis[n]=dis[n<<1]=0; 55 q.push(n); 56 q.push(n+n); 57 while(!q.empty()) 58 { 59 int d=q.front();q.pop(); 60 vis[d]=0; 61 for(int i=head[d] ; i ; i=E[i].next ) 62 { 63 int v=E[i].v; 64 int w=E[i].w; 65 if(dis[v]>dis[d]+w) 66 { 67 dis[v]=dis[d]+w; 68 if(!vis[v]) 69 { 70 vis[v]=1; 71 q.push(v); 72 } 73 } 74 } 75 } 76 } 77 void getans() 78 { 79 for(int i=1 ; i<=num ; ++i ) 80 { 81 if(dis[pos[i]]<ans) 82 { 83 ans=dis[pos[i]]; 84 memcpy(ansch,name[i],sizeof(name[i])); 85 } 86 if(dis[pos[i]]==ans) 87 if(ansch>name[i]) 88 memcpy(ansch,name[i],sizeof(name[i])); 89 } 90 puts(ansch); 91 printf("%d",ans); 92 } 93 int main() 94 { 95 int k; 96 scanf("%d%d",&n,&m); 97 for(int i=1 ; i<=m ; ++i ){x[i]=read();y[i]=read();z[i]=read();} 98 scanf("%d",&num); 99 for(int i=1 ; i<=num ; ++i ){k=read();odd[k]=1;} 100 scanf("%d",&num); 101 for(int i=1 ; i<=num ; ++i ){k=read();even[k]=1;} 102 scanf("%d",&num); 103 for(int i=1 ; i<=num ; ++i ) 104 { 105 pos[i]=read(); 106 scanf("%s",name[i]); 107 } 108 add(); 109 spfa(); 110 getans(); 111 return 0; 112 }
标签:运算 字典序 using 答案 char ++i algorithm 为什么 字符
原文地址:http://www.cnblogs.com/fujudge/p/7506509.html