poj3411Paid Roads(dfs)
想偷点懒用矩阵存 一直WA 后来看讨论说有重边改为邻接表 这题边可能走了不止一次 我设的最多两次可过
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define INF 0xfffffff
struct node
{
int u,v,w1,w2,d,next;
}ed[];
int head[];
int n,m,p[][],ans;
int vis[],t;
void init()
{
t = ;
memset(head,-,sizeof(head));
}
void add(int u,int v,int w1,int w2,int d)
{
ed[t].u = u;
ed[t].v = v;
ed[t].w1 = w1;
ed[t].w2 = w2;
ed[t].d = d;
ed[t].next = head[u];
head[u] = t++;
}
void dfs(int u,int sum)
{
int i,s=sum;
if(sum>ans)
return ;
if(u==n)
{
ans = sum;
return ;
}
for(i = head[u] ; i!= -; i = ed[i].next)
{
int v = ed[i].v;
int tt = vis[v];
if(p[u][v]<)
{
vis[v] = ;
p[u][v]++;
if(ed[i].d==u||ed[i].d==i||vis[ed[i].d])
{
sum+=min(ed[i].w1,ed[i].w2);
}
else
{
sum+=ed[i].w2;
}
dfs(v,sum);
sum = s;
p[u][v]--;
vis[v] = tt;
}
}
}
int main()
{
int a,b,c,pp,r;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
memset(vis,,sizeof(vis));
memset(p,,sizeof(p));
ans = INF;
while(m--)
{
scanf("%d%d%d%d%d",&a,&b,&c,&pp,&r);
add(a,b,pp,r,c);
}
vis[] = ;
dfs(,);
if(ans!=INF)
printf("%d\n",ans);
else
puts("impossible");
}
return ;
}
poj3411Paid Roads(dfs)的更多相关文章
- Codeforces Round #369 (Div. 2) D. Directed Roads dfs求某个联通块的在环上的点的数量
D. Directed Roads ZS the Coder and Chris the Baboon has explored Udayland for quite some time. The ...
- CodeForces #369 div2 D Directed Roads DFS
题目链接:D Directed Roads 题意:给出n个点和n条边,n条边一定都是从1~n点出发的有向边.这个图被认为是有环的,现在问你有多少个边的set,满足对这个set里的所有边恰好反转一次(方 ...
- codeforces 711D D. Directed Roads(dfs)
题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #369 (Div. 2) D. Directed Roads (DFS)
D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- csu 1930 roads(DFS)
Description Once upon a time there was a strange kingdom, the kingdom had n cities which were connec ...
- Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂
题目链接:http://codeforces.com/problemset/problem/711/D D. Directed Roads time limit per test 2 seconds ...
- lightoj 1049 - One Way Roads(dfs)
Time Limit: 0.5 second(s) Memory Limit: 32 MB Nowadays the one-way traffic is introduced all over th ...
- POJ 3411 Paid Roads(DFS)
题目链接 点和边 都很少,确定一个界限,爆搜即可.判断点到达注意一下,如果之前已经到了,就不用回溯了,如果之前没到过,要回溯. #include <cstring> #include &l ...
- Codeforces 711 D. Directed Roads (DFS判环)
题目链接:http://codeforces.com/problemset/problem/711/D 给你一个n个节点n条边的有向图,可以把一条边反向,现在问有多少种方式可以使这个图没有环. 每个连 ...
随机推荐
- javascript 事件 第23节
<html> <head> <title>DOM对象</title> <style type="text/css"> t ...
- 简化版的Flappy Bird开发过程(不使用第三方框架)
目录 .1构造世界 .2在世界中添加元素 .3碰撞检测 .4添加动画特效 .5总结 .0 开始之前 之前曾经用Html5/JavaScript/CSS实现过2048,用Cocos2d-html5/Ch ...
- Css颜色定义的方法汇总color属性设置方式
颜色的定义方式用rgb()里面带上十进制的数字来定义. color:rgb(211,123,135); 用预定义的颜色名称. color:red; rgba()最后一个参数是不透明度. color:r ...
- HDU 4764 Stone(博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4764 题目大意:Tang和Jiang玩石子游戏,给定n个石子,每次取[1,k]个石子,最先取完的人失败 ...
- Linux Terminal命令
Linux Terminal命令 1.Ctrl + a 回到命令行の「行首/head」. 2.Ctrl + e 回到命令行の「行尾/tail」, ctrl + end. 3.Ctrl + w 後向/b ...
- c#编写简单计算器
刚接触c#,依照惯例,写个简单的计算器,只写了加法,乘法,其他的类似,编辑器用的vs2008 首先打开vs ,新建c#的Windows窗体应用程序,接下来的项目的名称是WindowsFormsAppl ...
- win/linux 下使用 psutil 获取进程 CPU / memory / IO 占用信息
psutil - A cross-platform process and system utilities module for Python 1. 安装 pip 安装即可. windows 下需要 ...
- js中的referrer返回上一页使用介绍
js中的referrer的用法举例. js完整代码: <script language="javascript"> var refer=document.refer ...
- apache设置映射文件夹的配置方法
在apache的配置文件中加入以下配置 Alias /uploadImage F:/upload <Directory F:/upload/UploadFiles> Option ...
- C语言小结之结构类型
C语言小结之结构类型 @刁钻的游戏 (1)枚举型类型enum COLOR {BLACK,RED,BLUE};//声明一种新的数据类型,其值分别为0,1,2但是用BLACK/RED/BLUE代表也可以这 ...