记录编号 314437 评测结果 AAAAAAAAAA
题目名称 [HZOI 2016]在路上 最终得分 100
用户昵称 GravatarAntiLeaf 是否通过 通过
代码语言 C++ 运行时间 10.848 s
提交时间 2016-10-03 14:13:13 内存使用 182.66 MiB
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define INF ((~((1)<<(31)))>>(1))
#define bit(i) ((1)<<((i)-(1)))
using namespace std;
const int maxn=20010,maxm=200010;
struct edge{int to,dis,prev;}e[maxm<<1];
struct node{
	int x,dis;
	node(int a=0,int b=0):x(a),dis(b){}
	bool operator<(const node &b)const{return dis>b.dis;}
};
void addedge(int,int,int);
void Dijkstra(int,int*);
void dfs(int,int);
int n,m,c,last[maxn],len=0,dis[22][maxn],f[22][1<<21],pre[22]={0},ans=~(1<<31);
bool vis[maxn];
int main(){
	freopen("atr.in","r",stdin);
	freopen("atr.out","w",stdout);
	scanf("%d%d%d",&n,&m,&c);c++;
	while(m--){
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		addedge(x,y,z);
		addedge(y,x,z);
	}
	scanf("%d",&m);
	while(m--){
		int x,y;
		scanf("%d%d",&x,&y);
		pre[y]|=bit(x);
	}
	for(int i=1;i<=c;i++){
		Dijkstra(i,dis[i]);
		if(i!=1)pre[i]|=bit(1);
	}
	for(int i=1;i<=c;i++)for(int j=0;j!=bit(c+1);j++)f[i][j]=-1;
	for(int i=2;i<=c;i++){
		dfs(i,bit(c+1)-1);
		ans=min(ans,f[i][bit(c+1)-1]+dis[i][n]);
	}
	if(c==1)ans=dis[1][n];
	printf("%d",ans);
	fclose(stdin);
	fclose(stdout);
	return 0;
}
void addedge(int x,int y,int z){
	e[++len].to=y;
	e[len].dis=z;
	e[len].prev=last[x];
	last[x]=len;
}
void Dijkstra(int x,int *dis){
	memset(dis,63,sizeof(int)*(n+5));
	dis[x]=0;
	memset(vis,0,sizeof(vis));
	priority_queue<node>q;
	q.push(node(x,0));
	while(!q.empty()){
		node t=q.top();
		q.pop();
		x=t.x;
		if(vis[x])continue;
		vis[x]=true;
		for(int i=last[x];i;i=e[i].prev){
			if(vis[e[i].to])continue;
			if(dis[e[i].to]>dis[x]+e[i].dis){
				dis[e[i].to]=dis[x]+e[i].dis;
				q.push(node(e[i].to,dis[e[i].to]));
			}
		}
	}
}
void dfs(int i,int j){
	if((pre[i]&j)!=pre[i]){
		f[i][j]=INF;
		return;
	}
	if(j==bit(i)){
		f[i][j]=0;
		return;
	}
	if(!(j&bit(i))){
		f[i][j]=INF;
		return;
	}
	if(f[i][j]!=-1)return;
	f[i][j]=INF;
	for(int k=1;k<=c;k++){
		if(k==i||!(j&bit(k)))continue;
		dfs(k,j^bit(i));
		f[i][j]=min(f[i][j],f[k][j^bit(i)]+dis[k][i]);
	}
}
/*
8 15 4
1 2 3
1 3 4
1 4 4
1 6 2
1 7 3
2 3 6
2 4 2
2 5 2
3 4 3
3 6 3
3 8 6
4 5 2
4 8 6
5 7 4
5 8 6
3
2 3
3 4
3 5
Answer:
19
*/