HDU 4360

题意很好理解。

由于点是可以重复到达的,但可能每次经过路径的标志不一样,所以可以设每个点有四种状态"L”,'O','V','E'。然后按这些状态进行求最短路,当然是SPFA了。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#define LL __int64
using namespace std;
const LL inf=1ll<<58;
LL dis[1350][4];
int cnt[1350][4];
LL n1j[4];
int n,m;
int hed,tail;
int que[5350000];
bool inq[1350];
struct Edge{
	int u,v,nxt;
	LL c;
	char t;
}edge[30000];
int tot;
int head[1350];

void addedge(int u,int v,int c,char t){
	edge[tot].u=u;
	edge[tot].v=v;
	edge[tot].c=c;
	edge[tot].t=t;
	edge[tot].nxt=head[u];
	head[u]=tot++;
}

int isure(char c){
	if(c=='L') return 0;
	else if (c=='O') return 1;
	else if(c=='V') return 2;
	return 3;
}

void spfa(){
	hed=tail=0;
	memset(inq,false,sizeof(inq));
	inq[1]=true;
	que[tail++]=1;
	while(hed<tail){
		int u=que[hed++]; inq[u]=false;
		for(int e=head[u];e!=-1;e=edge[e].nxt){
			int v=edge[e].v,nt=isure(edge[e].t);
			if(dis[u][nt]+edge[e].c<=dis[v][(nt+1)%4]){
				bool flag=false;
				if(dis[v][(nt+1)%4]>dis[u][nt]+edge[e].c){
					dis[v][(nt+1)%4]=dis[u][nt]+edge[e].c;
					cnt[v][(nt+1)%4]=cnt[u][nt]+1;
					flag=true;
				}
				else{
					if(cnt[v][(nt+1)%4]<cnt[u][nt]+1){
						cnt[v][(nt+1)%4]=cnt[u][nt]+1;
						flag=true;
					}
					else continue;
				}
				if(flag){
					if(!inq[v]){
						inq[v]=true;
						que[tail++]=v;
					}
				}
			}
		}
	}
	if(dis[n][0]==inf||cnt[n][0]==0){
		printf("Binbin you disappoint Sangsang again, damn it!
");
	}
	else printf("Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters and finding %d LOVE strings at last.
",dis[n][0],cnt[n][0]/4);
	
}

int main(){
	int T,u,v,c; char t;
	int icase=0;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&n,&m);
		tot=0;
		memset(head,-1,sizeof(head));
		n1j[0]=n1j[1]=n1j[2]=n1j[3]=inf;
		for(int i=0;i<m;i++){
			scanf("%d %d %d %c",&u,&v,&c,&t);
			if(n==1){
				int f=isure(t);
				n1j[f]=min(n1j[f],(LL)c);
				continue;
			}
			addedge(u,v,c,t);
			addedge(v,u,c,t);
		}
		printf("Case %d: ",++icase);
		LL leng=0;
		for(int i=0;i<4;i++){
			leng+=n1j[i];
		}
		if(n==1){
			if(leng>=inf){ printf("Binbin you disappoint Sangsang again, damn it!
");}
			else{
				printf("Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters and finding 1 LOVE strings at last.
",leng);
			}
			continue;
		}
		
		for(int i=1;i<=n;i++){
			dis[i][0]=dis[i][1]=dis[i][2]=dis[i][3]=inf;
			cnt[i][0]=cnt[i][1]=cnt[i][2]=cnt[i][3]=0;
		}
		dis[1][0]=0;
		spfa();
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/jie-dcai/p/4424257.html