您的位置:首页 > 其它

HDU-4360 As long as Binbin loves Sangsang(Dijkstra)

2016-08-10 20:13 323 查看

As long as Binbin loves Sangsang

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 3415    Accepted Submission(s): 746


[align=left]Problem Description[/align]
Binbin misses Sangsang so much. He wants to meet with Sangsang as soon as possible.

Now Binbin downloads a map from ELGOOG.There are N (1<=N<=1,314) cities in the map and these cities are connected by M(0<=M<=13,520) bi-direct roads. Each road has a length L (1<=L<=1,314,520)and is marked using a unique ID, which is a letter fromthe string
“LOVE”!

Binbin rides a DONKEY, the donkey is so strange that it has to walk in the following sequence ‘L’->’O’->’V’->’E’->’L’->’O’->’V’->’E’->.... etc.

Can you tell Binbin how far the donkey has to walk in order to meet with Sangsang?

WARNING: Sangsang will feel unhappy if Binbin ride the donkey without a complete”LOVE” string.

Binbin is at node 1 and Sangsang is at node N.
 

[align=left]Input[/align]
The first line has an integer T(1<=T<=520), indicate how many test cases bellow.

Each test case begins with two integers N, M (N cities marked using an integer from 1…N and M roads).

Then following M lines, each line has four variables“U V L letter”, means that there is a road between city U,V(1<=U,V<=N) with length L and the letter marked is‘L’,’O’,’V’ or ‘E’

 

[align=left]Output[/align]
For each test case, output a string
1.  “Case ?: Binbin you disappoint Sangsang again, damn it!”

If Binbin failed to meet with Sangsang or the donkey can’t finish a path withthe full “LOVE” string.
2.  “Case ?: Cute Sangsang, Binbin will come with a donkey after travelling ? meters and finding ? LOVE strings at last.”

Of cause, the travel distance should be as short as possible, and at the same time the “LOVE” string should be as long as possible.

 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

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!

 

[align=left]Author[/align]
FZU
 

[align=left]Source[/align]
2012 Multi-University Training Contest 7 

这道题写了1天啊,结构体中重载运算符的时候大于号写成小于号一直没发现,TLE到想哭.

其实这道题挺水的,几乎就是一个裸Dijkstra,只要把1个点的状态分成4种就行

题意:给出n个点和m条边,点的编号为1~n,Binbin在1,Sangsang在n,Binbin骑着驴去找Sangsang,

这头奇怪的驴只会沿着标记为LOVE的路线走,比如L->O->V->E->L->O->V->E.

Sangsang要求Binbin走过的路线是个含有至少一个LOVE的完整的LOVE字符串,

要求Binbin找到Sangsang的路线最短,且经过的LOVE尽可能的多(注意:因为要是完整的LOVE字符串,所以必须的通过E到达终点)

题解:因为可能是通过L,O,V,E这四种不同的路线走到点u,所以u有四种状态,用d[u][mark]记录通过路线mark走到u时最短路径,

cnt[u][mark]记录走到u的步数,在d[u][mark]相同时,cnt[u][mark]要尽可能的大,最后如果不能走一个完整的LOVE字符串到n,
d
[mark]一定会是inf,否则如果d
[mark]<inf,则输出d
[mark]为距离,cnt
[mark]/4为LOVE的数量

#include<cstdio>
#include<algorithm>
#include<string.h>
#include<queue>
#include<vector>
#include<functional>
using namespace std;
const int maxn = 1333;
const int maxn_edge = 28888;
typedef long long LL;
struct Edge{
int u,v,mark,nxt;
LL cost;
}edge[maxn_edge];
struct node{
int u,mark;
LL cost;
node(int a,LL b,int c){u=a;cost=b;mark=c;}
bool operator<(const node& _A)const{
return cost>_A.cost;
}
};
int head[maxn],tot,cnt[maxn][4];
LL d[maxn][4];
void add(int u,int v,LL cost,int mark){
edge[tot].v=v;
edge[tot].cost=cost;
edge[tot].mark=mark;
edge[tot].nxt=head[u];
head[u]=tot++;
}
int main(){
int T,n,m;
// freopen("in.txt","r",stdin);
scanf("%d",&T);
for(int cas=1;cas<=T;cas++){
printf("Case %d: ",cas);
tot=0;
memset(head,-1,sizeof(head));
memset(cnt,0,sizeof(cnt));
memset(d,0x3f,sizeof(d));
LL inf=d[0][0];
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
int u,v,mark;
LL cost;
char op[2];
scanf("%d%d%I64d%s",&u,&v,&cost,op);
if(op[0]=='L') mark=0;   //分别给不同的路径标记一个数值
else if(op[0]=='O') mark=1;
else if(op[0]=='V') mark=2;
else if(op[0]=='E') mark=3;
add(u,v,cost,mark);    //注意是无向边
add(v,u,cost,mark);
}
if(m<4) {printf("Binbin you disappoint Sangsang again, damn it!\n");continue;} //其实这个判断不要也没关系
priority_queue<node>q;
for(int i=head[1];~i;i=edge[i].nxt){    //因为是从1出发的,一开始没有任何路径进入1,所以要特殊处理
Edge e=edge[i];
if(e.mark==0){
d[e.v][e.mark]=min(d[e.v][e.mark],e.cost);
cnt[e.v][e.mark]=1;
q.push(node(e.v,e.cost,e.mark));
}
}

while(!q.empty()){
node p=q.top();q.pop();
if(d[p.u][p.mark]<p.cost) continue;
int u=p.u;
for(int i=head[u];~i;i=edge[i].nxt){
if(edge[i].mark!=(p.mark+1)%4) continue;
int v=edge[i].v,mark=edge[i].mark,cost=edge[i].cost;
if(d[v][mark]>d[u][p.mark]+cost||(d[v][mark]==d[u][p.mark]+cost&&cnt[v][mark]<cnt[u][p.mark]+1)){
d[v][mark]=d[u][p.mark]+cost;
cnt[v][mark]=cnt[u][p.mark]+1;
q.push(node(v,d[v][mark],mark));
}
}
}

if(d
[3]==inf) printf("Binbin you disappoint Sangsang again, damn it!\n");
else printf("Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters and finding %d LOVE strings at last.\n",d
[3],cnt
[3]/4);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: