链接

想偷点懒用矩阵存 一直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)的更多相关文章

  1. 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 ...

  2. CodeForces #369 div2 D Directed Roads DFS

    题目链接:D Directed Roads 题意:给出n个点和n条边,n条边一定都是从1~n点出发的有向边.这个图被认为是有环的,现在问你有多少个边的set,满足对这个set里的所有边恰好反转一次(方 ...

  3. codeforces 711D D. Directed Roads(dfs)

    题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  4. 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 ...

  5. csu 1930 roads(DFS)

    Description Once upon a time there was a strange kingdom, the kingdom had n cities which were connec ...

  6. 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 ...

  7. 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 ...

  8. POJ 3411 Paid Roads(DFS)

    题目链接 点和边 都很少,确定一个界限,爆搜即可.判断点到达注意一下,如果之前已经到了,就不用回溯了,如果之前没到过,要回溯. #include <cstring> #include &l ...

  9. Codeforces 711 D. Directed Roads (DFS判环)

    题目链接:http://codeforces.com/problemset/problem/711/D 给你一个n个节点n条边的有向图,可以把一条边反向,现在问有多少种方式可以使这个图没有环. 每个连 ...

随机推荐

  1. WTL 中的常见问题汇总

    1.CRect,CPoint,CSize的使用 WTL提供了CString,CRect,CPoint和CSize,可能后来版本的ATL也提供了,WTL作者推荐使用ATL的实现,所以:#include ...

  2. 第28条:利用有限制通配符来提升API的灵活性

    参数化类型是不可变的.对两个不同类型T1和T2而言,List<T1>与List<T2>没有父子类型关系. 考虑: public class Stack<E> { p ...

  3. QQ登录网站接入

    QQ网站登录是一个非常常用的功能,网上有很多的资料,在此只做一个整理: QQ登录接入也在不断的升级,目前我发布的是2.1,很多资料里显示的那些繁杂的步骤已经不需要了: 第一步需要先申请,申请地址如下: ...

  4. Ajax跨域请求——PHP服务端处理

    header('Access-Control-Allow-Origin:*'); // 响应类型 header('Access-Control-Allow-Methods:POST'); // 响应头 ...

  5. Visual C++ 对话框增加菜单栏

    1.添加菜单资源     在resourceview视图中右击选择insert,添加一个菜单资源IDR_MENU1,同时设定好响应的菜单项,例 如:         菜单1               ...

  6. UIScrollView解决touchesBegan等方法不能触发的解方案

    新建一个类继承自UIScrollView  并重写下面的方法 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [su ...

  7. 获取iOS设备型号的方法总结

    三种常用的办法获取iOS设备的型号: 1. [UIDevice currentDevice].model (推荐): 2. uname(struct utsname *name) ,使用此函数需要#i ...

  8. 在类库中无法使用ConfigurationManager

    需要先引用DLL文件: C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.configuration.dll 然后才可以使用 System.Co ...

  9. poj 1631 Bridging signals (二分||DP||最长递增子序列)

    Bridging signals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9234   Accepted: 5037 ...

  10. 【记录】让人淡疼的BUG之参数传送错误

    前言 面试的时候往往容易被面试官问到:“说说你遇到过的比较重大或经典的Bug有哪些,能说一说吗?”我被问时脑海的反应是:“尼玛,这个我从来没有刻意记!一时半会咋想得起来,然后还是没想起来或者是随意给了 ...