URAL 1741 Communication Fiend(最短路径)
Description
Input
Output
题目大意:有一个软件,要从1升级到n。每个升级有一个花费,用了P之后就不能再用L,求最小花费。
思路:正解是DP?不管。我们用最短路。建双层图,对于a→b L,在第一层建一条边。对于a→b P,从第一层的a建一条边到第二层的b,再从第二层的a建一条边到第二层的b。对于a→b C,第一层建一条边,第二层建一条边。再从第一层的n建一条边到第二层的n,费用为0。那么就保证了走过了P之后不会再走L,用SPFA求个最短路圆满解决。个人认为比D好写多了。我们要把图论发扬光大O(∩_∩)O~
代码(31MS):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL; const int MAXN = ;
const int MAXE = MAXN * ; int head[MAXN];
int to[MAXE], next[MAXE], cost[MAXE];
int n, m, st, ed, ecnt; void init() {
memset(head, , sizeof(head));
ecnt = ;
} void add_edge(int u, int v, int c) {
to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
//printf("%d->%d %d\n", u, v, c);
} char s[]; void input() {
scanf("%d%d", &n, &m);
int a, b, c;
for(int i = ; i < m; ++i) {
scanf("%d%d%d%s", &a, &b, &c, s);
if(*s == 'P') {
add_edge(a, b + n, c);
add_edge(a + n, b + n, c);
}
if(*s == 'L') {
add_edge(a, b, c);
}
if(*s == 'C') {
add_edge(a, b, c);
add_edge(a + n, b + n, c);
}
}
add_edge(n, n + n, );
st = , ed = * n;
} LL dis[MAXN];
bool vis[MAXN]; void SPFA() {
memset(dis, , sizeof(dis));
memset(vis, , sizeof(vis));
queue<int> que; que.push(st);
dis[st] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
vis[u] = false;
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(dis[v] == - || dis[v] > dis[u] + cost[p]) {
dis[v] = dis[u] + cost[p];
if(!vis[v]) que.push(v);
vis[v] = true;
}
}
}
} void output() {
if(dis[ed] == -) puts("Offline");
else {
puts("Online");
cout<<dis[ed]<<endl;
}
} int main() {
init();
input();
SPFA();
output();
}
URAL 1741 Communication Fiend(最短路径)的更多相关文章
- DP/最短路 URAL 1741 Communication Fiend
题目传送门 /* 题意:程序从1到n版本升级,正版+正版->正版,正版+盗版->盗版,盗版+盗版->盗版 正版+破解版->正版,盗版+破解版->盗版 DP:每种情况考虑一 ...
- Ural 1741 Communication Fiend(隐式图+虚拟节点最短路)
1741. Communication Fiend Time limit: 1.0 second Memory limit: 64 MB Kolya has returned from a summe ...
- URAL 1741 Communication Fiend
URAL 1741 思路: dp 状态:dp[i][1]表示到第i个版本为正版的最少流量花费 dp[i][0]表示到第i个版本为盗版的最少流量花费 初始状态:dp[1][0]=dp[0][0]=0 目 ...
- 1741. Communication Fiend(dp)
刷个简单的DP缓缓心情 1A #include <iostream> #include<cstdio> #include<cstring> #include< ...
- URAL DP第一发
列表: URAL 1225 Flags URAL 1009 K-based Numbers URAL 1119 Metro URAL 1146 Maximum Sum URAL 1203 Scient ...
- URAL 1297 Palindrome 后缀数组
D - Palindrome Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- URAL 1297 最长回文子串(后缀数组)
1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...
- POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom /ZOJ 1291 MPI Maelstrom (最短路径)
POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom ...
- Johnson 全源最短路径算法
解决单源最短路径问题(Single Source Shortest Paths Problem)的算法包括: Dijkstra 单源最短路径算法:时间复杂度为 O(E + VlogV),要求权值非负: ...
随机推荐
- 推荐几款基于vue的使用插件
1.muse-ui ★6042 - 三端样式一致的响应式 UI 库 2.vuetify ★11169 - 为移动而生的Vue JS 2组件框架 3.Vux ★12969- 基于Vue和WeUI的组件库 ...
- Redis集群的安装和使用
- 史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)(Finchley版本)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f3-feign/ 本文出自方志朋的博客 上一篇文章,讲述了如 ...
- 自动计算UITableViewCell高度2(CGRect约束)
1.先创建model .h #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface LBDNe ...
- webpack和sass功能简介
1.webpack webpack 是一个打包工具,为什么需要打包?因为有的人的脚本开发语言可能是 CoffeeScript 或者是 TypeScript,样式开发工具可能是 Less 或者 Sass ...
- Git----将本地代码推送到远程仓库
1.初始化本地 git init 2.添加文件 -A等于 -. 和-a的集合 git add -A 3.提交 git commit -m 'add' 4.关联到远程库 git remote add o ...
- Yaf学习(一)----Linux安装Yaf
1.简介 Yaf,全称 Yet Another Framework,是一个高性能的PHP开发框架,采用PHP扩展实现(c语言).Blablablabla....... 2.环境 2.1 虚拟机 虚拟机 ...
- Learning Experience of Big Data: Learn to install CentOs 6.5 on my laptop
I have learnt some experience about Big Data during my summer vocation,I was told that The first thi ...
- ssh 远程命令
远程拷贝文件,scp -r 的常用方法: 1.使用该命令的前提条件要求目标主机已经成功安装openssh-server 如没有安装使用 sudo apt-get install openssh-ser ...
- C# 设置程序最小化到任务栏右下角,鼠标左键单击还原,右键提示关闭程序
首先设置程序最小化到任务栏右下角 先给窗口添加一个notifyIcon控件 为notifyIcon控件设置ICO图标(不设置图标将无法在任务栏显示) 给notifyIcon控件添加点击事件 然后是最小 ...