2 4 4 1 2 1 L 2 1 1 O 1 3 1 V 3 4 1 E 4 4 1 2 1 L 2 3 1 O 3 4 1 V 4 1 1 E
Case 1: Cute Sangsang, Binbin will come with a donkey after travelling 4 meters and finding 1 LOVE strings at last. Case 2: Binbin you disappoint Sangsang again, damn it!
题意描述:
n个点,m条边,起点为1,终点为n(注意!n可能等于1)的无向图(注意!无向图)。只能按照L->O->V->E->L->O->...的顺序走,求到达n点的最短长度和组成LOVE的最大个数。
解题思路:
SPFA,多一个dp数组记录,特判n=1的情况,路径长度可能超int。
参考代码:
#include<map> #include<stack> #include<queue> #include<cmath> #include<vector> #include<cctype> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const double eps=1e-10; const long long INF=1e17; const int MAXN=1500; typedef long long LL; struct edge { int to,love; LL value; edge(int a,LL b,int c) { to=a; value=b; love=c; } }; vector<edge>G[MAXN]; int n,m,dp[MAXN][4]; LL mincost[MAXN][4]; bool used[MAXN]; void SPFA(int start) { for(int i=1;i<=n;i++) { for(int j=0;j<4;j++) { mincost[i][j]=INF; dp[i][j]=0; } used[i]=false; } queue<int>q; q.push(start); mincost[start][0]=0; used[start]=true; while(!q.empty()) { int now=q.front(); q.pop(); used[now]=false; for(int i=0;i<G[now].size();i++) { edge e=G[now][i]; int next=(e.love+1)%4; if(mincost[e.to][next]>mincost[now][e.love]+e.value) { mincost[e.to][next]=mincost[now][e.love]+e.value; dp[e.to][next]=dp[now][e.love]+1; if(!used[e.to]) { q.push(e.to); used[e.to]=true; } } else if(mincost[e.to][next]==mincost[now][e.love]+e.value) dp[e.to][next]=max(dp[e.to][next],dp[now][e.love]+1); } } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif // ONLINE_JUDGE int tcase,f=0; scanf("%d",&tcase); while(tcase--) { int spe=0; LL spec[4]; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) G[i].clear(); memset(spec,0,sizeof(spec)); for(int i=1;i<=m;i++) { int from,to,lov; LL val; char l[4]; scanf("%d%d%lld%s",&from,&to,&val,l); if(l[0]=='L') lov=0; else if(l[0]=='O') lov=1; else if(l[0]=='V') lov=2; else if(l[0]=='E') lov=3; G[from].push_back(edge(to,val,lov)); G[to].push_back(edge(from,val,lov)); if(n==1)//特判只有一个点的情况 { if(!spec[lov]) { spec[lov]=val; spe++; } else spec[lov]=min(spec[lov],val); } } printf("Case %d: ",++f); if(n==1) { if(spe==4) { LL sum=spec[0]+spec[1]+spec[2]+spec[3]; printf("Cute Sangsang, Binbin will come with a donkey after travelling %lld meters and finding 1 LOVE strings at last.\n",sum); } else printf("Binbin you disappoint Sangsang again, damn it!\n"); continue; } SPFA(1); if(mincost[n][0]==INF||dp[n][0]<4)//只需要计算完整的LOVE printf("Binbin you disappoint Sangsang again, damn it!\n"); else printf("Cute Sangsang, Binbin will come with a donkey after travelling %lld meters and finding %d LOVE strings at last.\n",mincost[n][0],dp[n][0]/4); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 4360 As long as Binbin loves Sangsang(SPFA)
原文地址:http://blog.csdn.net/noooooorth/article/details/47319163