Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6472 | Accepted: 2398 |
Description
A network of m roads connects N cities (numbered from 1 toN). There may be more than one road connecting one city with another. Some of the roads are paid. There are two ways to pay for travel on a paid roadi from city ai to city bi:
- in advance, in a city ci (which may or may not be the same asai);
- after the travel, in the city bi.
The payment is Pi in the first case and Ri in the second case.
Write a program to find a minimal-cost route from the city 1 to the city N.
Input
The first line of the input contains the values of N and m. Each of the following m lines describes one road by specifying the values ofai, bi, ci,Pi, Ri (1 ≤ i ≤ m). Adjacent values on the same line are separated by one or more spaces. All values are integers, 1 ≤m, N ≤ 10, 0 ≤ Pi , Ri ≤ 100,Pi ≤ Ri (1 ≤ i ≤ m).
Output
The first and only line of the file must contain the minimal possible cost of a trip from the city 1 to the cityN. If the trip is not possible for any reason, the line must contain the word ‘impossible’.
Sample Input
4 5 1 2 1 10 10 2 3 1 30 50 3 4 3 80 80 2 1 2 10 10 1 3 2 10 50
Sample Output
110
Source
分析:为什么每个点最多经过四次?
#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
vector <int> a[11];
int y[11],c[11],p[11],r[11],now[11],ans,n,m,x;
void dfs(int now[11],int x,int cost)
{
if(cost >= ans) return;
if(now[n])
{
ans=cost;
return;
}
for(int i=0;i < a[x].size();i++)
{
int u=a[x][i];
if(now[y[u]] <= 4)
if(now[c[u]])
{
now[y[u]]++;
dfs(now,y[u],cost+p[u]);
now[y[u]]--;
}
else
{
now[y[u]]++;
dfs(now,y[u],cost+r[u]);
now[y[u]]--;
}
}
}
int main()
{
cin>>n>>m;
for(int i=1;i <= m;i++)
{
cin>>x>>y[i]>>c[i]>>p[i]>>r[i];
a[x].push_back(i);
}
ans=2147483647;
now[1]=1;
dfs(now,1,0);
if(ans != 2147483647) cout<<ans<<endl;
else cout<<"impossible"<<endl;
}
状压最短路版
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#define MAXN 1047483647
using namespace std;
vector <int> a[11];
struct point
{
int x,state,v;
point()
{
x=0;
state=0;
v=0;
}
};
bool operator < ( point a,point b)
{
return a.v > b.v;
}
point makepoint(int x,int state,int v)
{
point now;
now.x=x,now.state=state,now.v=v;
return now;
}
priority_queue< point > q;
int y[11],c[11],p[11],r[11],d[11][1024],ans,n,m,x;
bool done[11][1024];
int main()
{
cin>>n>>m;
for(int i=1;i <= m;i++)
{
cin>>x>>y[i]>>c[i]>>p[i]>>r[i];
a[x].push_back(i);
}
for(int i=0;i < 11;i++)
for(int j=0;j < 1024;j++)
d[i][j]=MAXN;
memset(done,0,sizeof(done));
d[1][1]=0;
q.push(makepoint(1,1,0));
ans=MAXN;
while(!q.empty())
{
point u=q.top();
q.pop();
if(done[u.x][u.state]) continue;
done[u.x][u.state]=true;
if(u.x == n) ans=min(u.v,ans);
for(int i=0;i < a[u.x].size();i++)
{
int v=a[u.x][i],cost;
if( u.state & (1 << (c[v]-1)) ) cost=p[v];
else cost=r[v];
int now=u.state | ( 1 << (y[v]-1)) ;
if(d[y[v]][now] > cost+u.v)
{
d[y[v]][now]=cost+u.v;
q.push(makepoint(y[v],now,cost+u.v));
}
}
}
if(ans != MAXN) cout<<ans<<endl;
else cout<<"impossible"<<endl;
}