| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
注意:在这个题目中,每个城市可以经过多次,且每条边也可能走多次 (我就是没有想到每条边还可能走多次。。。卡了一上午)这个要好好想想#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 15;
struct edge
{
int v,c,p,r,next;
}edges[maxn];
int head[maxn];
int vispath[maxn];
//bool vism[maxn];
int n,m,e,ans;
void addedge(int u,int v,int c,int p,int r)
{
edges[e].v = v; edges[e].c = c; edges[e].p = p; edges[e].r = r;
edges[e].next = head[u];
head[u] = e++;
}
void dfs(int u,int cost)
{
if(cost > ans) return;
if(u == n)
{
if(cost < ans) ans = cost;
return;
}
for(int i=head[u];i!=-1;i=edges[i].next)
{
int v = edges[i].v;
if(vispath[v]<=3)
{
vispath[v]++;
if(vispath[edges[i].c]>0) dfs(v,cost+edges[i].p);
else dfs(v,cost+edges[i].r);
vispath[v]--;
}
}
}
int main()
{
int u,v,c,p,r;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-1,sizeof(head));
e = 0;
for(int i=0;i<m;i++)
{
scanf("%d%d%d%d%d",&u,&v,&c,&p,&r);
addedge(u,v,c,p,r);
}
ans = 99999999;
memset(vispath,0,sizeof(vispath));
memset(vism,false,sizeof(vism));
vispath[1] = 1;
dfs(1,0);
if(ans < 99999999) printf("%d\n",ans);
else printf("impossible\n");
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator