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!\n");
}
else printf("Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters and finding %d LOVE strings at last.\n",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!\n");}
else{
printf("Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters and finding 1 LOVE strings at last.\n",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;
}
HDU 4360的更多相关文章
- HDU 4360 As long as Binbin loves Sangsang spfa
题意: 给定n个点m条边的无向图 每次必须沿着LOVE走,到终点时必须是完整的LOVE,且至少走出一个LOVE, 问这样情况下最短路是多少,在一样短情况下最多的LOVE个数是多少. 有自环. #inc ...
- 8-12-COMPETITION
链接:最短路 A.HDU 2544 最短路 算是最基础的题目了吧.............我采用的是Dijkstra算法....... 代码: #include <iostream> ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
随机推荐
- Java并发编程系列之Semaphore详解
简单介绍 我们以饭店为例,假设饭店只有三个座位,一开始三个座位都是空的.这时如果同时来了三个客人,服务员人允许他们进去用餐,然后对外说暂无座位.后来的客人必须在门口等待,直到有客人离开.这时,如果有一 ...
- POJ 1584 计算几何
思路: 求一遍凸包 用三角形面积(叉积求一下)/边长 求出来高,跟半径比一比 坑点:凸包上三点共线 //By SiriusRen #include <cmath> #include < ...
- 题解报告:hdu 1233 还是畅通工程
Problem Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能 ...
- Select2插件ajax方式加载数据并刷新页面数据回显
今天在优化项目当中,有个要在下拉框中搜索数据的需求:最后选择使用selec2进行开发: 官网:http://select2.github.io/ 演示: 准备工作: 文件需要引入select2.ful ...
- 【转】Java 集合系列04之 fail-fast总结(通过ArrayList来说明fail-fast的原理、解决办法)
概要 前面,我们已经学习了ArrayList.接下来,我们以ArrayList为例,对Iterator的fail-fast机制进行了解.内容包括::1 fail-fast简介2 fail-fast示例 ...
- JS——行内式注册事件
html中行内调用function的时候,是通过window调用的function,所以打印this等于打印window,所以在使用行内注册事件时务必传入参数this <!DOCTYPE htm ...
- JS——对象创建
1.原始创建 <script> person = new Object();//不要var person.firstname = "Bill"; person.last ...
- 初识关系型数据库(SQL)与非关系型数据库(NOSQL)
一.关系型数据库(SQL): Mysql,oracle 特点:数据和数据之间,表和字段之间,表和表之间是存在关系的 例如:部门表 001部分, 员工表 001 用户表,用户名.密码 分类表 和 商 ...
- if({1,0}, , )
=VLOOKUP(F2,IF({1,0},D2:D10,C2:C10),2,),用F2作为查找条件,对D列进行查找,如果改成{0,1}则是对C列为查找范围.返回内存数组,对F2进行精确查找. 为什么写 ...
- CAD插入非等比例的图块
主要用到函数说明: _DMxDrawX::InsertBlock 向控件数据库中插入一个图块,不用它插入匿名块.详细说明如下: 参数 说明 BSTR pszDwgFileName 图块定义的dwg 文 ...